Is there a bug in stride(from:through:by:) or is it me?

Consider this:

let r = stride(from: 5, through: 1, by: -1)
print(r.contains(3)) // false

Pass it through an array and now it works:

let r = stride(from: 5, through: 1, by: -1).map { $0 }
print(r.contains(3)) // true

Going forward also works:

let r = stride(from: 1, through: 5, by: 1)
print(r.contains(3)) // true

Doesn't make any sense to me. Am I misunderstanding how the stride function works?
Using the latest Xcode 13 with Swift 5.5.


Yeah, this looks like a bug: StrideThrough implements a custom override of _customContainsEquatableElement that assumes that the start is always less than the end:

  public func _customContainsEquatableElement(
    _ element: Element
  ) -> Bool? {
    if element < _start || _end < element {
      return false
    return nil

Please file an issue on and post the number here.

Incidentally, StrideTo is affected by the same bug.

Oof. Yes, @seanmrich, please file a bug and link here. I think the following PR will fix it but regardless it’ll be good to have a bug to reference.

Bug filed: SR-15384


Looks like a fix was submitted same day. Great job @xwu!

Terms of Service

Privacy Policy

Cookie Policy