i think the reality is this does not scale, because there are always going to be use cases that donât reach the bar for inclusion in the standard library, but would still benefit from a readable literal syntax.
sometimes it is not needed. when i pitched this in 2018, the compiler was not very smart, and the difference between the following really mattered:
func test1(_ terminal:UInt8) -> Void?
{
switch Unicode.Scalar.init(terminal)
{
case "/", "\\": return ()
default: return nil
}
}
func test2(_ terminal:UInt8) -> Void?
{
switch terminal
{
// '/' '\'
case 0x2f, 0x5c: return ()
default: return nil
}
}
but today LLVM can figure out they are the same, so i personally plan on rewriting a lot of these functions to use Unicode.Scalar instead of UInt8:
main:
xor eax, eax
ret
output.test1(Swift.UInt8) -> ()?:
cmp dil, 47
setne cl
cmp dil, 92
setne al
and al, cl
ret
output.test2(Swift.UInt8) -> ()?:
jmp (output.test1(Swift.UInt8) -> ()?)
__swift_reflection_version:
.short 3
but just because the compiler has gotten better over the past five years does not mean this feature is not needed anymore, because it still trips and falls on more complex cases, and i think unless you are someone who has memorized their ASCII tables, the following is still quite hard to decipher without the comments:
@inlinable public mutating
func next() -> UInt8?
{
while let digit:UInt8 = self.iterator.next(), digit != 0x3D // '='
{
switch digit
{
case 0x41 ... 0x5a: // A-Z
return digit - 0x41
case 0x61 ... 0x7a: // a-z
return digit - 0x61 + 26
case 0x30 ... 0x39: // 0-9
return digit - 0x30 + 52
case 0x2b: // +
return 62
case 0x2f: // /
return 63
default:
continue
}
}
return nil
}
extension Base64
{
public
enum Digits
{
public static
let ascii:[UInt8] =
[
0x41,
0x42,
0x43,
0x44,
0x45,
0x46,
0x47,
0x48,
0x49,
0x4a,
0x4b,
0x4c,
0x4d,
0x4e,
0x4f,
0x50,
0x51,
0x52,
0x53,
0x54,
0x55,
0x56,
0x57,
0x58,
0x59,
0x5a,
0x61,
0x62,
0x63,
0x64,
0x65,
0x66,
0x67,
0x68,
0x69,
0x6a,
0x6b,
0x6c,
0x6d,
0x6e,
0x6f,
0x70,
0x71,
0x72,
0x73,
0x74,
0x75,
0x76,
0x77,
0x78,
0x79,
0x7a,
0x30,
0x31,
0x32,
0x33,
0x34,
0x35,
0x36,
0x37,
0x38,
0x39,
0x2b,
0x2f,
]
}
}