I am trying to extend Character
to conform to Strideable
in order to create a CountableClosedRange
of Character
types. In the end, I would like to have something like this which prints the whole alphabet:
("A"..."Z").forEach{
print($0)
}
For the time being, I am using UnicodeScalar
types to calculate the distance between two characters. Because a scalar isn't available from a Character
type, I need to create a String from the Character, get the first scalar's value, and calculate the distance between them:
extension Character: Strideable {
func distance(to other: Character) -> Character.Stride {
return abs(String(self).unicodeScalars.first?.value - String(other).unicodeScalars.first!.value)
}
func advanced(by n: Character.Stride) -> Character {
return Character(UnicodeScalar(String(self).unicodeScalars.first!.value + n))
}
}
Even with this, I get the error that Character
does not conform to protocol Strideable
and _Strideable
. The compiler does not appear to be picking up the Stride
associated type which comes with Strideable
:
public protocol Strideable : Comparable {
/// A type that can represent the distance between two values of `Self`.
associatedtype Stride : SignedNumber
// ...
}
What am I missing?