Now that the battle for integer convertibility seems pretty much lost, I wonder if adding a few well chosen operators to the standard library couldn’t scratch the itch the proposal was trying to address...
For array initialisation I’d suggest:
extension Array where Element: FixedWidthInteger {
@available(swift 5.1)
public init(scalars: String) {
self = scalars.unicodeScalars.map { Element($0.value) }
}
}
let hex = [Int8](scalars: "0123456789abcdef")
For comparison you could simply add:
@_transparent
@available(swift 5.1)
public func ==<T: FixedWidthInteger> (lhs: T, rhs: Unicode.Scalar) -> Bool {
return lhs == rhs.value
}
if cString.advanced(by: 2).pointee == “b" {
For use in switches the following is enough:
@_transparent
@available(swift 5.1)
public func ~=<T: FixedWidthInteger> (pattern: Unicode.Scalar, value: T) -> Bool {
return pattern.value == value
}
@available(swift 5.1)
public func ~=<T: FixedWidthInteger> (pattern: ClosedRange<Unicode.Scalar>, value: T) -> Bool {
return pattern.contains(Unicode.Scalar(UInt32(value))!)
}
let digit = UInt8(ascii: "1")
switch(digit) {
case "8":
print("Hello1")
case "1" ... "2":
print("Hello2")
default:
break
}
Operators that might be useful can be added without opening the flood gates to nonsense expressions.
@_transparent
@available(swift 5.1)
public func -<T: FixedWidthInteger> (lhs: T, rhs: Unicode.Scalar) -> T {
return lhs - T(rhs.value)
}
print(digit-“0")