> On Nov 23, 2017, at 3:07 PM, Tony Allevato via swift-evolution > <swift-evolution@swift.org> wrote: > > This could be solved by extending the existing string literal handling and > letting type inference do the rest. The real problem here is that > `UInt8(ascii: X)` is annoying to write when you're dealing with a large > amount of low-level data. > > If UInt8 conformed to ExpressibleByUnicodeScalarLiteral, you could get most > of the way there—you'd just have to have it fail at runtime for anything > outside 0...127. But then you could write `let c: UInt8 = "x"` and it would > just work. > > Failing at runtime is undesirable though
There is another way to handle this: we can diagnose it the same way we diagnose statically identifiable overflow of arithmetic operations: (swift) 128 as Int8 <REPL Input>:1:1: error: integer literal '128' overflows when stored into 'Int8' 128 as Int8 ^ (swift) 1+127 as Int8 <REPL Input>:1:2: error: arithmetic operation '1 + 127' (on type 'Int8') results in an overflow 1+127 as Int8 ~^~~~ The way this happens is through constant folding at the SIL level, which emits diagnostics when they constant fold the “should trap” bit on these operations to true. The code in question is in lib/SILOptimizer/Mandatory/ConstantPropagation.cpp -Chris
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution