I've found that the Swift compiler name lookup behavior in pattern matching is rather odd.
// The full type declaration is below.
struct Bytes { ... }
enum ASCII: UInt8 {
case lineFeed = 10
}
switch bytes {
case ASCII.lineFeed: print("match") // error: enum case 'lineFeed' is not a member of type 'Bytes'
}
@jrose, after reading your post I have just realised that the code in the OP is expected to be legal. I did not know that the Swift can become cryptic.
switch Bytes([10]) {
case .lineFeed:
print("match")
default:
print("not match")
}
I don't see the connection between Bytes ([10]) and case .lineFeed.
the OP has a new type to enumerate standard ASCII constants and wants to simplify the use site to be able using "switch bytes" and pattern match bytes to ascii constants, with the further twist that the bytes array is not of type ASCII but of type UInt8.
It’s normally only used with types (Int.self) but it works on anything. In particular it can be useful when a key path is required. Here it’s mostly just a workaround.