> On Jun 4, 2016, at 11:54 AM, Chris Lattner <clatt...@apple.com> wrote:
> On Jun 2, 2016, at 9:08 AM, John McCall via swift-evolution 
> <swift-evolution@swift.org <mailto:swift-evolution@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.
>> 
>> In my opinion, using this initializer-call syntax to build an 
>> explicitly-typed literal is an obvious and natural choice with several 
>> advantages over the "as" syntax.
> 
> I completely agree that this is a problem that we need to solve.  In addition 
> to the trap of using [U]Int64 values on 32-bit targets, it is embarrassing 
> that we reject (on all targets):
> 
>       let x = UInt64(0x8000_0000_0000_0000)
> 
> and require people to use the less obvious syntax:
> 
>       let x = 0x1000_0000_0000_0000 as UInt64
> 
>> Therefore, I propose that we adopt the following typing rule:
> 
> I’m sorry of this has already been covered down-thread (just getting caught 
> up now, and haven’t read it all), but this seems like a LOT of magic in the 
> type checker to solve this problem.

It was somewhat covered elsewhere in the thread, but this particular idea is 
new, I think.

> Can’t we just require that literal convertibles implement an initializer that 
> the type checker will already consider to be more specific than any of the 
> other overloads?  This would eliminate the need for magic like this in the 
> type checker.  Right now, we have this:
> 
> public protocol IntegerLiteralConvertible {
>   associatedtype IntegerLiteralType : _BuiltinIntegerLiteralConvertible
>   init(integerLiteral value: IntegerLiteralType)
> }
> 
> Change it to be an unlabeled requirement like this probably isn’t enough to 
> make it privileged in the case of ambiguity:

Right.

> public protocol IntegerLiteralConvertible {
>   associatedtype IntegerLiteralType : _BuiltinIntegerLiteralConvertible
>   init(_ value: IntegerLiteralType)
> }
> 
> but perhaps we could have:
> 
> public protocol IntegerLiteralConvertible {
>   associatedtype IntegerLiteralType : _BuiltinIntegerLiteralConvertible
>   init(integerLiteral value: IntegerLiteralType)
>   init<T : IntegerLiteralConvertible>(_ value: T)
> }
> 
> and get the type checker to consider the later one to be a“more specific” 
> match than the other overloads, when confronted with a literal?

Er.  We definitely don't want to say that every integer-literal-convertible 
type has to be initializable from an arbitrary value of an arbitrary other 
integer-literal-convertible type.  That's not an implementable requirement, nor 
should it be.

The idea of making a special unlabelled initializer that's preferred by the 
type-checker came up earlier in the thread; the more workable proposals 
included declaring it with @literal or changing the type to some magic 
Literal<T> type.  I just don't think it's a good idea because it's still 
basically the same type-checker magic on the expression side to recognize that 
we should favor the case, and then it adds extra complexity on the declaration 
side; plus the different literal protocols start to interfere with each other 
if one type implements more than one of them.

If you're worried about the complexity of adding new constraints, we could 
start with a more modest rule that only applies the special case when the 
callee is syntactically a type reference rather than e.g. an arbitrary 
expression of metatype value.  But changing the ambiguity-resolution rules is 
actually a lot more complex than just adding a new constraint.

John.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to