I have this extension:
extension Range {
func foo() { // called with (0..<5).foo()
let bar = 0..<5
print(bar) // 0..<5
print(self) // 0..<5
print(type(of: bar)) // Range<Int>
print(type(of: self)) // Range<Int>
for i in bar { // :)
}
for j in self { // :(
}
}
}
For some reason, the first loop is fine, but the second isn't. I get a compile time error saying
Type 'Bound' does not conform to protocol 'Strideable'
Something that's also interesting to note here is that assigning bar
with self
effects the error to pop up in the first loop.
Make it explicit to the compiler that the Bound
is Strideable
:
extension Range where Element: Strideable {
...
}
Defining a Range
only requires the Bound
to only be Comparable
. See the actual implementation here. Comparable
is the minimum requirement to define a Range
.
There are two types of ranges :
CoutableRange
s: These are ranges over types (Bound
) that conform to Strideable
and use integer steps between elements. These ranges are treated like a Sequence
, and thus can be used in for
loop.
Normal ranges: These include Range
and ClosedRange
with Comparable
elements only, and thus can't be iterated over.
This is explicitly given in this comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With