Unfortunately this pitch wandered into a legislative labyrinth that I don't have the wit to find the way out of, nor the wisdom to know when to give up. So.. I've started casting about for alternatives and found an extension and a new Array initialiser that I believe solve the bulk of what I was looking to achieve.
First, looking at the "awkward code" @beccadax mentioned:
If you define just this one simple extension: (Edited)
extension FixedWidthInteger {
/// Basic equality operators
@_transparent
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
return i == s.value
}
@_transparent
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
return i != s.value
}
/// Used in switch statements
@_transparent
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
return i == s.value
}
/// Maybe useful now and then
@_transparent
public static func - (i: Self, s: Unicode.Scalar) -> Self {
return i - Self(s.value)
}
}
The code could be transformed from:
switch self.previous {
case UInt8(ascii: " "), UInt8(ascii: "\r"), UInt8(ascii: "\n"), UInt8(ascii: "\t"), // whitespace
UInt8(ascii: "("), UInt8(ascii: "["), UInt8(ascii: "{"), // opening delimiters
UInt8(ascii: ","), UInt8(ascii: ";"), UInt8(ascii: ":"), // expression separators
0: // whitespace / last char in file
return false
to
switch self.previous {
case " ", "\r", "\n", "\t", "(", "[", "{", ",", ";", ":", // expression separators
0: // whitespace / last char in file
return false
This would be a candidate for inclusion in the standard library IMHO as it is an additive change that shouldn't involve collateral damage to the language it is so finely targeted. The compiler tests run through and there are only 12 failures where the diagnostic changed in some tests for invalid code. I have further tested 1000 or so Swift packages from the Swift Package Index and didn't see problems.
Coming full circle on how these pitches started out:
It may be better to simply introduce a new initialiser on Array
s of FixedWidthInteger
s; something along the lines of:
extension Array where Element: FixedWidthInteger {
/// Initialise an Integer array of "characters"
@inline(__always)
public init(unicode: String, default: UInt32 = 0) {
self.init(unicode.map {
let scalars = $0.unicodeScalars
if scalars.count == 1,
let v = scalars.first?.value,
v <= Element.max {
return Element(v)
}
return Element(`default`)
})
}
}
So the code above would become:
let hexcodes = [UInt8](unicode: "0123456789abcdef")
Between these two suggestions I believe the majority of use cases I felt were poorly served by current Swift find a solution. I've put together a small Swift Package so you can try these ideas out but would hope we could find a path for these to find their way into the stdlib: