[swift-evolution] Proposal: 'T(literal)' should construct T using the appropriate literal protocol if possible

Russ Bishop xenadu at gmail.com
Thu Jun 2 16:13:13 CDT 2016


> On Jun 2, 2016, at 9:08 AM, John McCall via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The official way to build a literal of a specific type is to write the literal in an explicitly-typed context, like so:
>     let x: UInt16 = 7
> or
>     let x = 7 as UInt16
> 
> Nonetheless, programmers often try the following:
>     UInt16(7)
> 
> Unfortunately, this does not attempt to construct the value using the appropriate literal protocol; it instead performs overload resolution using the standard rules, i.e. considering only single-argument unlabelled initializers of a type which conforms to IntegerLiteralConvertible.  Often this leads to static ambiguities or, worse, causes the literal to be built using a default type (such as Int); this may have semantically very different results which are only caught at runtime.


+1 to the proposal; I’m embarrassed to admit I thought it worked this way already. 

Any type that wants more control shouldn’t be adopting the literal convertible protocols anyway.

Russ

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160602/19bc0729/attachment.html>


More information about the swift-evolution mailing list