# Proposal: 'T(literal)' should construct T using the appropriate literal protocol if possible

**URL:** <https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861>\
**Category:** Discussion\
**Created:** [June 2, 2016, 4:08pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861 "2016-06-02T16:08:53Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Austin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/austin/32/1196_2.png) [@Austin](https://forums.swift.org/u/Austin)\
**Post date:** [June 2, 2016, 8:44pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/41 "2016-06-02T20:44:30Z")

</div>

That makes sense. Thanks for sharing your reasoning.

> **···**
>
> On Thu, Jun 2, 2016 at 1:38 PM, John McCall \<rjmccall@apple.com\> wrote:
> 
> > On Jun 2, 2016, at 11:55 AM, Austin Zheng \<austinzheng@gmail.com\> wrote:  
> > I think we should actually go further.
> > 
> > If this proposal is accepted, disallow "as" as a way of specifying the  
> > type to construct from a literal.
> > 
> > I see no reason to restrict "as" like this. We're not going to stop using  
> > type context as a way of determining the type of a literal, and "as" is a  
> > general feature for providing type context.
> > 
> > If this proposal isn't accepted, disallow using literal values as the  
> > argument to one-unlabeled-argument constructors.
> > 
> > I can't imagine accepting this, either; it would be a major regression in  
> > the usefulness of unlabeled initializers. At best, this would be  
> > appropriate only when the type conforms to an appropriate literal protocol.
> > 
> > In general, Swift gives unlabeled initializers an idiomatic meaning: they  
> > coerce the argument to the target type in a nominally value-preserving  
> > way. One way of viewing this proposal is that it recognizes that there is  
> > an obvious way to do that when the target type supports being directly  
> > formed from the given kind of literal. But if the target type doesn't  
> > support that, coercing a value of some other appropriate literal type is  
> > still a completely reasonable behavior.
> > 
> > John.
> > 
> > Austin
> > 
> > On Thu, Jun 2, 2016 at 11:46 AM, Austin Zheng \<austinzheng@gmail.com\> \> wrote:
> > 
> > > +1.
> > > 
> > > The primary advantage is that it aligns the language semantics with how  
> > > most programmers expect this common C-language-family idiom to behave and  
> > > removes a potential source of silently wrong code.
> > > 
> > > The primary disadvantage is that it introduces special-case behavior to  
> > > certain types of initializers (although, to be fair, this special-case  
> > > behavior is easily recognizable: unlabeled one-argument initializer with a  
> > > literal as the argument).
> > > 
> > > I think the advantage outweighs the disadvantage.
> > > 
> > > This problem should be addressed one way or another. I prefer this  
> > > solution, but if it is rejected for whatever reason we should at least  
> > > explicitly outlaw A(literal) syntax in favor of "literal as A".
> > > 
> > > Austin
> > > 
> > > On Thu, Jun 2, 2016 at 9:08 AM, John McCall via swift-evolution \< \>\> 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:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;let x: UInt16 = 7  
> > > > or  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;let x = 7 as UInt16
> > > > 
> > > > Nonetheless, programmers often try the following:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;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. However, even if you disagree, it's clear  
> > > > that programmers are going to continue to independently try to use it, so  
> > > > it's really unfortunate for it to be subtly wrong.
> > > > 
> > > > Therefore, I propose that we adopt the following typing rule:
> > > > 
> > > > &nbsp;&nbsp;Given a function call expression of the form A(B) (that is, an  
> > > > \*expr-call\* with a single, unlabelled argument) where B is an  
> > > > \*expr-literal\* or \*expr-collection\*, if A has type T.Type for some type  
> > > > T and there is a declared conformance of T to an appropriate literal  
> > > > protocol for B, then the expression is always resolves as a literal  
> > > > construction of type T (as if the expression were written "B as A") rather  
> > > > than as a general initializer call.
> > > > 
> > > > Formally, this would be a special form of the argument conversion  
> > > > constraint, since the type of the expression A may not be immediately known.
> > > > 
> > > > Note that, as specified, it is possible to suppress this typing rule by  
> > > > wrapping the literal in parentheses. This might seem distasteful; it would  
> > > > be easy enough to allow the form of B to include extra parentheses. It's  
> > > > potentially useful to have a way to suppress this rule and get a normal  
> > > > construction, but there are several other ways of getting that effect, such  
> > > > as explicitly typing the literal argument (e.g. writing "A(Int(B))").
> > > > 
> > > > A conditional conformance counts as a declared conformance even if the  
> > > > generic arguments are known to not satisfy the conditional conformance.  
> > > > This permits the applicability of the rule to be decided without having to  
> > > > first decide the type arguments, which greatly simplifies the type-checking  
> > > > problem (and may be necessary for soundness; I didn't explore this in  
> > > > depth, but it certainly feels like a very nasty sort of dependence). We  
> > > > could potentially weaken this for cases where A is a direct type reference  
> > > > with bound parameters, e.g. Foo\<Int\>() or the same with a typealias, but  
> > > > I think there's some benefit from having a simpler specification, both for  
> > > > the implementation and for the explicability of the model.
> > > > 
> > > > John.
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 2, 2016, 11:27pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/42 "2016-06-02T23:27:21Z")

</div>

> > So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> 
> I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.

My point is that a literal doesn't really have a pre-existing type. Your explanation relies on a very strange model in which we assign it an arbitrary type just so we can introduce unwanted semantic conversions from that type.

> > You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> 
> If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.

This does not work. Literal construction is not necessarily as simple as converting from an existing type, and it's important to use a semantically distinguished initializer.

> But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.

It sounds like you're not in favor of the proposal, then.

John.

> **···**
>
> > On Jun 2, 2016, at 4:22 PM, Brent Royal-Gordon \<brent@architechies.com\> wrote:

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 2, 2016, 11:33pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/43 "2016-06-02T23:33:51Z")

</div>

The IntegerLiteral type idea might be worth exploring. It does seem to  
provide some additional consistency. For example, wasn't it clarified on  
this list just recently that literals don't have a type and adopt one based  
on context? It'd be nice to point out that 42 is an IntegerLiteral when  
explaining that it's not an Int.

> **···**
>
> On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > \> So, you think that this syntax is enticing to new developers who  
> > naturally think that the feature works the way that I'm proposing it should  
> > work, and you think that the right solution is to make the syntax illegal  
> > so that you can more conveniently tell them it doesn't work that way? :)
> > 
> > I think the difference between a cast (which merely reinterprets a value  
> > as a compatible type) and a fullwidth conversion (which creates a similar  
> > instance of an incompatible type) is very important to understanding how to  
> > write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > 
> > \> You can still tell them that it's a struct and you're calling an  
> > initializer on it; it's just that the initializer chosen is the special  
> > literal initializer because the argument is a literal.
> > 
> > If you're planning to change `IntegerLiteralConvertible` and friends to  
> > require a fullwidth conversion initializer like `init(\_ value:  
> > IntegerLiteralType)`, then this is simply an overload resolution rule. In  
> > that case, I think your proposal is fine.
> > 
> > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`,  
> > I don't think that's a good idea. Parameter labels are supposed to be  
> > significant; we don't want to lose that.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 3, 2016, 6:09pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/44 "2016-06-03T18:09:06Z")

</div>

> \> Like I said, the standard library and compiler conspire to make sure that easy cases like this are caught at compile time, but that would not help non-standard types that conform to IntegerLiteralConvertible.  
> \>  
> \> Also, even for standard types, the syntax only works statically if the literal fits in the range of Int, which may not be a superset of the desired type. For example, UInt64(0x10000000000) would not work on a 32-bit platform. It is diagnosed statically, however.
> 
> I believe I understand the problem you described, but I really can't figure out how this problem can produce a unexpected behavior and run-time errors, as was stated in your initial message. So, this is why I was asking for any code that can prove this. The example with UInt64(0x10000000000) on 32bit systems will raise error at \_compilation\_ time. Could someone provide any code to illustrate the possible problems at run-time?

It's sufficient to do the same thing as the standard library, just without the @transparent tricks that enable static detection of the failure:

&nbsp;&nbsp;struct WidgetCount : IntegerLiteralConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;var value: UInt64  
&nbsp;&nbsp;&nbsp;&nbsp;init(integerLiteral value: UInt64) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = value  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;init(\_ value: Int) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = UInt64(value)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}

&nbsp;&nbsp;let count = WidgetCount(-1)

John.

> **···**
>
> > On Jun 2, 2016, at 10:28 PM, Vladimir.S \<svabox@gmail.com\> wrote:
> 
> > For others, who not really understand the issue(probably I'm not the one, I hope ;-) ) : If we'd have Int128 type, we can't create an instance of it in this form:  
> > let x = Int128(92233720368547758070)  
> > (92233720368547758070 == Int.max \* 10)  
> > as '92233720368547758070' literal will be treated always as of Int type.
> > 
> > In more general description, the difference between UIntN(xxx) and yyy as UIntN is that xxx will be treated as Int(so it can't be greater than Int.max for example) then this Int will be sent to UIntN(:Int) initializer and then we have UIntN as a result of call to initializer; yyy will be treated as UIntN literal just by its definition, no calling to any initializer, yyy can be of any value allowed for UIntN.
> > 
> > But again, could someone help with examples when this can produce problems at run-time?
> > 
> > On 03.06.2016 1:12, John McCall wrote:
> > 
> > > > On Jun 2, 2016, at 2:36 PM, Vladimir.S \<svabox@gmail.com\> wrote:  
> > > > Well, I understand that it seems like there is some problem with UIntN() initialization, but can't find any simple code that will demonstrate this..
> > > > 
> > > > All below works as expected:
> > > > 
> > > > var x1: Int32 = 0  
> > > > var x2 = Int32(0)
> > > > 
> > > > print(x1.dynamicType, x2.dynamicType) // Int32 Int32
> > > > 
> > > > // integer overflows when converted from 'Int' to 'UInt16'  
> > > > //var x = UInt16(100\_000)  
> > > > //var x = UInt16(-10)
> > > > 
> > > > // negative integer cannot be converted to unsigned type 'UInt64'  
> > > > // var x = UInt64(-1)
> > > > 
> > > > So, what code will produce some unexpected behavior / error at runtime?
> > > 
> > > Like I said, the standard library and compiler conspire to make sure that easy cases like this are caught at compile time, but that would not help non-standard types that conform to IntegerLiteralConvertible.
> > > 
> > > Also, even for standard types, the syntax only works statically if the literal fits in the range of Int, which may not be a superset of the desired type. For example, UInt64(0x10000000000) would not work on a 32-bit platform. It is diagnosed statically, however.
> > > 
> > > John.
> > > 
> > > > On 03.06.2016 0:25, John McCall wrote:
> > > > 
> > > > > > On Jun 2, 2016, at 1:56 PM, Vladimir.S \<svabox@gmail.com\> wrote:
> > > > > > 
> > > > > > > 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.
> > > > > > 
> > > > > > Seems like I'm very slow today.. Could you present a couple of examples where such initialization(like UInt16(7)) can produce some unexpected behavior / error at runtime?
> > > > > 
> > > > > UIntN has unlabeled initializers taking all of the standard integer types, including itself. The literal type will therefore get defaulted to Int. The legal range of values for Int may not be a superset of the legal range of values for UIntN. If the literal is in the legal range for an Int but not for the target type, this might trap at runtime. Now, for a built-in integer type like UInt16, we will recognize that the coercion always traps and emit an error at compile-time, but this generally won't apply to other types.
> > > > > 
> > > > > John.
> > > > > 
> > > > > > On 02.06.2016 19:08, John McCall via swift-evolution 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. However, even if you disagree, it's clear  
> > > > > > > that programmers are going to continue to independently try to use it, so  
> > > > > > > it's really unfortunate for it to be subtly wrong.
> > > > > > > 
> > > > > > > Therefore, I propose that we adopt the following typing rule:
> > > > > > > 
> > > > > > > Given a function call expression of the form A(B) (that is, an  
> > > > > > > /expr-call/ with a single, unlabelled argument) where B is  
> > > > > > > an /expr-literal/ or /expr-collection/, if A has type T.Type for some type  
> > > > > > > T and there is a declared conformance of T to an appropriate literal  
> > > > > > > protocol for B, then the expression is always resolves as a literal  
> > > > > > > construction of type T (as if the expression were written "B as A") rather  
> > > > > > > than as a general initializer call.
> > > > > > > 
> > > > > > > Formally, this would be a special form of the argument conversion  
> > > > > > > constraint, since the type of the expression A may not be immediately known.
> > > > > > > 
> > > > > > > Note that, as specified, it is possible to suppress this typing rule by  
> > > > > > > wrapping the literal in parentheses. This might seem distasteful; it would  
> > > > > > > be easy enough to allow the form of B to include extra parentheses. It's  
> > > > > > > potentially useful to have a way to suppress this rule and get a normal  
> > > > > > > construction, but there are several other ways of getting that effect, such  
> > > > > > > as explicitly typing the literal argument (e.g. writing "A(Int(B))").
> > > > > > > 
> > > > > > > A conditional conformance counts as a declared conformance even if the  
> > > > > > > generic arguments are known to not satisfy the conditional conformance.  
> > > > > > > This permits the applicability of the rule to be decided without having to  
> > > > > > > first decide the type arguments, which greatly simplifies the type-checking  
> > > > > > > problem (and may be necessary for soundness; I didn't explore this in  
> > > > > > > depth, but it certainly feels like a very nasty sort of dependence). We  
> > > > > > > could potentially weaken this for cases where A is a direct type reference  
> > > > > > > with bound parameters, e.g. Foo\<Int\>() or the same with a typealias, but  
> > > > > > > I think there's some benefit from having a simpler specification, both for  
> > > > > > > the implementation and for the explicability of the model.
> > > > > > > 
> > > > > > > John.
> > > > > > > 
> > > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > > swift-evolution mailing list  
> > > > > > > swift-evolution@swift.org  
> > > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > .

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 8, 2016, 5:23pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/45 "2016-06-08T17:23:04Z")

</div>

> > > > The official way to build a literal of a specific type is to write the  
> > > > literal in an explicitly-typed context, like so:
> > 
> > > > &nbsp;&nbsp;let x: UInt16 = 7  
> > > > or  
> > > > &nbsp;&nbsp;let x = 7 as UInt16
> > > > 
> > > > Nonetheless, programmers often try the following:  
> > > > &nbsp;&nbsp;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. However, even if you disagree, it's  
> > > > clear that programmers are going to continue to independently try to  
> > > > use it, so it's really unfortunate for it to be subtly wrong.
> > > > 
> > > > Therefore, I propose that we adopt the following typing rule:
> > > > 
> > > > Given a function call expression of the form A(B) (that is, an  
> > > > expr-call with a single, unlabelled argument) where B is an  
> > > > expr-literal or expr-collection, if A has type T.Type for some type T  
> > > > and there is a declared conformance of T to an appropriate literal  
> > > > protocol for B, then the expression is always resolves as a literal  
> > > > construction of type T (as if the expression were written "B as A")  
> > > > rather than as a general initializer call.
> > > > 
> > > > Formally, this would be a special form of the argument conversion  
> > > > constraint, since the type of the expression A may not be immediately  
> > > > known.
> > > 
> > > I realize this is somewhat tangential, but... IMO this may not be entirely  
> > > about literals.
> > > 
> > > We have a standard that full-width type conversions are written as a  
> > > label-free initializer  
> > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt;%5C).  
> > > I believe that is partly responsible for setting up the expectation that  
> > > Int(42) works as one would expect. It gets ultra-weird when you can  
> > > convert from type A to type B using B(someA) but you can't write  
> > > B(someB). We should automatically generate a label-free “copy  
> > > initializer” for value types, to complete implementation of the expected  
> > > mental model.
> > 
> > That may also be a good idea, but it won't magically be preferred for  
> > literal construction if the type has any other constructors of  
> > literal-convertible type.
> 
> I know. I'm saying, fixing this for literals without giving value types  
> copy initializers leaves us with only a partial realization of a larger  
> mental model to which I believe people are programming.

That's fair. Would you like me to incorporate that into my proposal, then? I see the relation, but it's a pretty significant jump in scope.

John.

> **···**
>
> > On Jun 7, 2016, at 4:25 PM, Dave Abrahams \<dabrahams@apple.com\> wrote:  
> > on Tue Jun 07 2016, John McCall \<rjmccall-AT-apple.com\> wrote:
> > 
> > > > On Jun 5, 2016, at 5:18 PM, Dave Abrahams via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > on Thu Jun 02 2016, John McCall \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> 
> > > > > Note that, as specified, it is possible to suppress this typing rule  
> > > > > by wrapping the literal in parentheses. This might seem distasteful;  
> > > > > it would be easy enough to allow the form of B to include extra  
> > > > > parentheses. It's potentially useful to have a way to suppress this  
> > > > > rule and get a normal construction, but there are several other ways  
> > > > > of getting that effect, such as explicitly typing the literal argument  
> > > > > (e.g. writing "A(Int(B))").
> > > > > 
> > > > > A conditional conformance counts as a declared conformance even if the  
> > > > > generic arguments are known to not satisfy the conditional  
> > > > > conformance. This permits the applicability of the rule to be decided  
> > > > > without having to first decide the type arguments, which greatly  
> > > > > simplifies the type-checking problem (and may be necessary for  
> > > > > soundness; I didn't explore this in depth, but it certainly feels like  
> > > > > a very nasty sort of dependence). We could potentially weaken this  
> > > > > for cases where A is a direct type reference with bound parameters,  
> > > > > e.g. Foo\<Int\>() or the same with a typealias, but I think there's  
> > > > > some benefit from having a simpler specification, both for the  
> > > > > implementation and for the explicability of the model.
> > > > > 
> > > > > John.  
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > --  
> > > > -Dave
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > --  
> > Dave

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 2, 2016, 11:51pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/46 "2016-06-02T23:51:48Z")

</div>

> The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.

I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.

John.

> **···**
>
> > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com\> wrote:
> 
> > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > 
> > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > 
> > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > 
> > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > 
> > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 3, 2016, 1:48am UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/47 "2016-06-03T01:48:51Z")

</div>

> > The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.
> 
> I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.

It sounds like the reason you don't want to remove the label is because it distinguishes the literal initializer and you don't want it called with a variable accidentally right?

What if we do something like this:

init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)

The idea here is that the IntegerLiteral type and friends are special types that have no members and cannot be used except by the literal convertible protocols and their conformances. There is nothing you can do with the value at all.

The trick is that by defaulting it to #literal we are indicating the compiler synthesizes the logical value, as with #file, #line, etc. The compiler would refuse to synthesize this value when a literal is not used. This means it is never selected when the user provides a variable to a converting initializer. If an independent initializer accepting a value of the same type also exists that would be selected. However, when a literal \*is\* used and the type conforms to the relevant literal convertible protocol the compiler always synthesized the value making it always the most specific overload.

Of course no runtime value would actually exist. This is just a logical value marking the fact that a literal was used to call the initializer.

This approach solves the same problem while retaining semantic consistency with the language (no label elision or short circuit of overload resolution). The magic is arguably a lot more restrained - types for which values can only be supplied by the compiler. We could artificially restrict usage of these types if we wanted to, but we wouldn't have to. Nothing could be accomplished by using the types anywhere else so nobody do so and it wouldn't be actively harmful to allow them to be used anywhere other types can be used. Only the ability to create values of the type needs to be restricted and we can already write types like that by marking the initializers private.

Any thoughts on this approach?

> **···**
>
> Sent from my iPad
> 
> > > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com\> wrote:
> 
> > John.
> > 
> > > > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > > > 
> > > > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > > 
> > > > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > > > 
> > > > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > > > 
> > > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > > > 
> > > > --  
> > > > Brent Royal-Gordon  
> > > > Architechies
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 3, 2016, 6:36pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/48 "2016-06-03T18:36:44Z")

</div>

> > > The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.
> > 
> > I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.
> 
> It sounds like the reason you don't want to remove the label is because it distinguishes the literal initializer and you don't want it called with a variable accidentally right?
> 
> What if we do something like this:
> 
> init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)
> 
> The idea here is that the IntegerLiteral type and friends are special types that have no members and cannot be used except by the literal convertible protocols and their conformances. There is nothing you can do with the value at all.
> 
> The trick is that by defaulting it to #literal we are indicating the compiler synthesizes the logical value, as with #file, #line, etc. The compiler would refuse to synthesize this value when a literal is not used. This means it is never selected when the user provides a variable to a converting initializer. If an independent initializer accepting a value of the same type also exists that would be selected. However, when a literal \*is\* used and the type conforms to the relevant literal convertible protocol the compiler always synthesized the value making it always the most specific overload.
> 
> Of course no runtime value would actually exist. This is just a logical value marking the fact that a literal was used to call the initializer.
> 
> This approach solves the same problem while retaining semantic consistency with the language (no label elision or short circuit of overload resolution). The magic is arguably a lot more restrained - types for which values can only be supplied by the compiler. We could artificially restrict usage of these types if we wanted to, but we wouldn't have to. Nothing could be accomplished by using the types anywhere else so nobody do so and it wouldn't be actively harmful to allow them to be used anywhere other types can be used. Only the ability to create values of the type needs to be restricted and we can already write types like that by marking the initializers private.
> 
> Any thoughts on this approach?

This is still a special-case type-checking rule, which means that it's still basically my proposal except, instead of just rewriting the call to use a labeled literal initializer, it rewrites the call to use a magic initializer which cannot be used by users because it requires arguments that users cannot create. I just don't understand the motivation here. It's not a simpler language model to use, explain, or implement; it purports to be conceptually simpler and less magical while actually inventing two magic new language concepts (IntegerLiteral and #literal) on top of the same special cases.

Literals are always going to involve some magic behind the scenes. The non-trivial literals, for example, all actually traffic through some sort of builtin protocol (e.g. BuiltinIntegerLiteralConvertible) which cannot be implemented by users and whose construction necessarily involves a private API contract between the standard library and compiler that we reserve the right to change at will without bringing in swift-evolution. All of the supposedly simple explanations eventually reach, "okay, but if Int isn't a special type, how does the literal turn into an Int in the first place", and that is just unanswerable, because it turns out that standard library types \*are\* actually special in the sense that they get to conform to BuiltinIntegerLiteralConvertible and nobody else does.

John.

> **···**
>
> > On Jun 2, 2016, at 6:48 PM, Matthew Johnson \<matthew@anandabits.com\> wrote:  
> > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > 
> > > > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com \<mailto:xiaodi.wu@gmail.com\>\> wrote:
> 
> > > John.
> > > 
> > > > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > > > 
> > > > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > > 
> > > > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > > > 
> > > > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > > > 
> > > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > > > 
> > > > --  
> > > > Brent Royal-Gordon  
> > > > Architechies
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 4, 2016, 12:31am UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/49 "2016-06-04T00:31:53Z")

</div>

John McCall wrote:

> I think that's a very promising way of thinking about literals. Writing  
> a literal creates a notional value whose type is the informal, infinite-  
> precise type of all integer/FP/collection/etc. literals, which (1) can be  
> implicitly converted to any type that implements the appropriate protocol  
> and (2) in fact \*must\* be converted to some such type (possibly the  
> default type for that literal) in order for the code to be executable.

Could you allow IntegerLiteralConvertible.IntegerLiteralType associatedtype  
to override the default Swift.IntegerLiteralType typealias iff there's more  
than one unlabelled init(\_:) to choose from? Then you can call the "correct"  
init(\_:) instead of calling init(integerLiteral:) as a "special case".

[stdlib/public/core/CompilerProtocols.swift]

&nbsp;&nbsp;&nbsp;&nbsp;/// Conforming types can be initialized with integer literals.  
&nbsp;&nbsp;&nbsp;&nbsp;public protocol IntegerLiteralConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype IntegerLiteralType : \_BuiltinIntegerLiteralConvertible  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/// Create an instance initialized to `value`.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(integerLiteral value: IntegerLiteralType)  
&nbsp;&nbsp;&nbsp;&nbsp;}

[stdlib/public/core/Policy.swift]

&nbsp;&nbsp;&nbsp;&nbsp;/// The default type for an otherwise-unconstrained integer literal.  
&nbsp;&nbsp;&nbsp;&nbsp;public typealias IntegerLiteralType = Int

-- Ben

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [June 6, 2016, 3:01pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/50 "2016-06-06T15:01:14Z")

</div>

> > > Like I said, the standard library and compiler conspire to make sure that easy cases like this are caught at compile time, but that would not help non-standard types that conform to IntegerLiteralConvertible.
> > > 
> > > Also, even for standard types, the syntax only works statically if the literal fits in the range of Int, which may not be a superset of the desired type. For example, UInt64(0x10000000000) would not work on a 32-bit platform. It is diagnosed statically, however.
> > 
> > I believe I understand the problem you described, but I really can't figure out how this problem can produce a unexpected behavior and run-time errors, as was stated in your initial message. So, this is why I was asking for any code that can prove this. The example with UInt64(0x10000000000) on 32bit systems will raise error at \_compilation\_ time. Could someone provide any code to illustrate the possible problems at run-time?
> 
> It's sufficient to do the same thing as the standard library, just without the @transparent tricks that enable static detection of the failure:

Thank you, but I was awaiting the example where using of UIntN/IntN() with integer literal produces \*unexpected\* error at run-time or unexpected behavior (as was stated in your initial message). I don't understand how the example you sent is connected with this. Could some one provide a sample code to understand in which situations we can have described problems when using UIntN/IntN with integer literals?

About your example : you have init(:Int), you sent -1 to it, UInt64(value) will generate \*expected\* run time error in this case.

> **···**
>
> On 03.06.2016 21:09, John McCall wrote:
> 
> > > On Jun 2, 2016, at 10:28 PM, Vladimir.S \<svabox@gmail.com\> wrote:
> 
> > &nbsp;&nbsp;struct WidgetCount : IntegerLiteralConvertible {  
> > &nbsp;&nbsp;&nbsp;&nbsp;var value: UInt64  
> > &nbsp;&nbsp;&nbsp;&nbsp;init(integerLiteral value: UInt64) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;init(\_ value: Int) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = UInt64(value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;let count = WidgetCount(-1)
> > 
> > John.
> > 
> > > For others, who not really understand the issue(probably I'm not the one, I hope ;-) ) : If we'd have Int128 type, we can't create an instance of it in this form:  
> > > let x = Int128(92233720368547758070)  
> > > (92233720368547758070 == Int.max \* 10)  
> > > as '92233720368547758070' literal will be treated always as of Int type.
> > > 
> > > In more general description, the difference between UIntN(xxx) and yyy as UIntN is that xxx will be treated as Int(so it can't be greater than Int.max for example) then this Int will be sent to UIntN(:Int) initializer and then we have UIntN as a result of call to initializer; yyy will be treated as UIntN literal just by its definition, no calling to any initializer, yyy can be of any value allowed for UIntN.
> > > 
> > > But again, could someone help with examples when this can produce problems at run-time?
> > > 
> > > On 03.06.2016 1:12, John McCall wrote:
> > > 
> > > > > On Jun 2, 2016, at 2:36 PM, Vladimir.S \<svabox@gmail.com\> wrote:  
> > > > > Well, I understand that it seems like there is some problem with UIntN() initialization, but can't find any simple code that will demonstrate this..
> > > > > 
> > > > > All below works as expected:
> > > > > 
> > > > > var x1: Int32 = 0  
> > > > > var x2 = Int32(0)
> > > > > 
> > > > > print(x1.dynamicType, x2.dynamicType) // Int32 Int32
> > > > > 
> > > > > // integer overflows when converted from 'Int' to 'UInt16'  
> > > > > //var x = UInt16(100\_000)  
> > > > > //var x = UInt16(-10)
> > > > > 
> > > > > // negative integer cannot be converted to unsigned type 'UInt64'  
> > > > > // var x = UInt64(-1)
> > > > > 
> > > > > So, what code will produce some unexpected behavior / error at runtime?
> > > > 
> > > > Like I said, the standard library and compiler conspire to make sure that easy cases like this are caught at compile time, but that would not help non-standard types that conform to IntegerLiteralConvertible.
> > > > 
> > > > Also, even for standard types, the syntax only works statically if the literal fits in the range of Int, which may not be a superset of the desired type. For example, UInt64(0x10000000000) would not work on a 32-bit platform. It is diagnosed statically, however.
> > > > 
> > > > John.
> > > > 
> > > > > On 03.06.2016 0:25, John McCall wrote:
> > > > > 
> > > > > > > On Jun 2, 2016, at 1:56 PM, Vladimir.S \<svabox@gmail.com\> wrote:
> > > > > > > 
> > > > > > > > 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.
> > > > > > > 
> > > > > > > Seems like I'm very slow today.. Could you present a couple of examples where such initialization(like UInt16(7)) can produce some unexpected behavior / error at runtime?
> > > > > > 
> > > > > > UIntN has unlabeled initializers taking all of the standard integer types, including itself. The literal type will therefore get defaulted to Int. The legal range of values for Int may not be a superset of the legal range of values for UIntN. If the literal is in the legal range for an Int but not for the target type, this might trap at runtime. Now, for a built-in integer type like UInt16, we will recognize that the coercion always traps and emit an error at compile-time, but this generally won't apply to other types.
> > > > > > 
> > > > > > John.
> > > > > > 
> > > > > > > On 02.06.2016 19:08, John McCall via swift-evolution 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. However, even if you disagree, it's clear  
> > > > > > > > that programmers are going to continue to independently try to use it, so  
> > > > > > > > it's really unfortunate for it to be subtly wrong.
> > > > > > > > 
> > > > > > > > Therefore, I propose that we adopt the following typing rule:
> > > > > > > > 
> > > > > > > > Given a function call expression of the form A(B) (that is, an  
> > > > > > > > /expr-call/ with a single, unlabelled argument) where B is  
> > > > > > > > an /expr-literal/ or /expr-collection/, if A has type T.Type for some type  
> > > > > > > > T and there is a declared conformance of T to an appropriate literal  
> > > > > > > > protocol for B, then the expression is always resolves as a literal  
> > > > > > > > construction of type T (as if the expression were written "B as A") rather  
> > > > > > > > than as a general initializer call.
> > > > > > > > 
> > > > > > > > Formally, this would be a special form of the argument conversion  
> > > > > > > > constraint, since the type of the expression A may not be immediately known.
> > > > > > > > 
> > > > > > > > Note that, as specified, it is possible to suppress this typing rule by  
> > > > > > > > wrapping the literal in parentheses. This might seem distasteful; it would  
> > > > > > > > be easy enough to allow the form of B to include extra parentheses. It's  
> > > > > > > > potentially useful to have a way to suppress this rule and get a normal  
> > > > > > > > construction, but there are several other ways of getting that effect, such  
> > > > > > > > as explicitly typing the literal argument (e.g. writing "A(Int(B))").
> > > > > > > > 
> > > > > > > > A conditional conformance counts as a declared conformance even if the  
> > > > > > > > generic arguments are known to not satisfy the conditional conformance.  
> > > > > > > > This permits the applicability of the rule to be decided without having to  
> > > > > > > > first decide the type arguments, which greatly simplifies the type-checking  
> > > > > > > > problem (and may be necessary for soundness; I didn't explore this in  
> > > > > > > > depth, but it certainly feels like a very nasty sort of dependence). We  
> > > > > > > > could potentially weaken this for cases where A is a direct type reference  
> > > > > > > > with bound parameters, e.g. Foo\<Int\>() or the same with a typealias, but  
> > > > > > > > I think there's some benefit from having a simpler specification, both for  
> > > > > > > > the implementation and for the explicability of the model.
> > > > > > > > 
> > > > > > > > John.
> > > > > > > > 
> > > > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > > > swift-evolution mailing list  
> > > > > > > > swift-evolution@swift.org  
> > > > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > .
> > 
> > .

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [June 8, 2016, 6:15pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/51 "2016-06-08T18:15:34Z")

</div>

I'm not sure it should be in the same proposal; I just want it to be a  
part of the discussion. We should be able to make incremental progress,  
but let's make sure we understand how our changes fit into the big picture.

> **···**
>
> on Wed Jun 08 2016, John McCall \<rjmccall-AT-apple.com\> wrote:
> 
> > > On Jun 7, 2016, at 4:25 PM, Dave Abrahams \<dabrahams@apple.com\> wrote:  
> > > on Tue Jun 07 2016, John McCall \<rjmccall-AT-apple.com\> wrote:
> > > 
> > > > > On Jun 5, 2016, at 5:18 PM, Dave Abrahams via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > > on Thu Jun 02 2016, John McCall \<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:
> > > > 
> > > > > > &nbsp;&nbsp;let x: UInt16 = 7  
> > > > > > or  
> > > > > > &nbsp;&nbsp;let x = 7 as UInt16
> > > > > > 
> > > > > > Nonetheless, programmers often try the following:  
> > > > > > &nbsp;&nbsp;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. However, even if you disagree, it's  
> > > > > > clear that programmers are going to continue to independently try to  
> > > > > > use it, so it's really unfortunate for it to be subtly wrong.
> > > > > > 
> > > > > > Therefore, I propose that we adopt the following typing rule:
> > > > > > 
> > > > > > Given a function call expression of the form A(B) (that is, an  
> > > > > > expr-call with a single, unlabelled argument) where B is an  
> > > > > > expr-literal or expr-collection, if A has type T.Type for some type T  
> > > > > > and there is a declared conformance of T to an appropriate literal  
> > > > > > protocol for B, then the expression is always resolves as a literal  
> > > > > > construction of type T (as if the expression were written "B as A")  
> > > > > > rather than as a general initializer call.
> > > > > > 
> > > > > > Formally, this would be a special form of the argument conversion  
> > > > > > constraint, since the type of the expression A may not be immediately  
> > > > > > known.
> > > > > 
> > > > > I realize this is somewhat tangential, but... IMO this may not be entirely  
> > > > > about literals.
> > > > > 
> > > > > We have a standard that full-width type conversions are written as a  
> > > > > label-free initializer  
> > > > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion)  
> > > > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt;&gt;%5C).  
> > > > > I believe that is partly responsible for setting up the expectation that  
> > > > > Int(42) works as one would expect. It gets ultra-weird when you can  
> > > > > convert from type A to type B using B(someA) but you can't write  
> > > > > B(someB). We should automatically generate a label-free “copy  
> > > > > initializer” for value types, to complete implementation of the expected  
> > > > > mental model.
> > > > 
> > > > That may also be a good idea, but it won't magically be preferred for  
> > > > literal construction if the type has any other constructors of  
> > > > literal-convertible type.
> > > 
> > > I know. I'm saying, fixing this for literals without giving value types  
> > > copy initializers leaves us with only a partial realization of a larger  
> > > mental model to which I believe people are programming.
> > 
> > That's fair. Would you like me to incorporate that into my proposal,  
> > then? I see the relation, but it's a pretty significant jump in  
> > scope.
> 
> --  
> Dave

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 3, 2016, 7:41pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/52 "2016-06-03T19:41:20Z")

</div>

> > > > The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.
> > > 
> > > I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.
> > 
> > It sounds like the reason you don't want to remove the label is because it distinguishes the literal initializer and you don't want it called with a variable accidentally right?
> > 
> > What if we do something like this:
> > 
> > init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)
> > 
> > The idea here is that the IntegerLiteral type and friends are special types that have no members and cannot be used except by the literal convertible protocols and their conformances. There is nothing you can do with the value at all.
> > 
> > The trick is that by defaulting it to #literal we are indicating the compiler synthesizes the logical value, as with #file, #line, etc. The compiler would refuse to synthesize this value when a literal is not used. This means it is never selected when the user provides a variable to a converting initializer. If an independent initializer accepting a value of the same type also exists that would be selected. However, when a literal \*is\* used and the type conforms to the relevant literal convertible protocol the compiler always synthesized the value making it always the most specific overload.
> > 
> > Of course no runtime value would actually exist. This is just a logical value marking the fact that a literal was used to call the initializer.
> > 
> > This approach solves the same problem while retaining semantic consistency with the language (no label elision or short circuit of overload resolution). The magic is arguably a lot more restrained - types for which values can only be supplied by the compiler. We could artificially restrict usage of these types if we wanted to, but we wouldn't have to. Nothing could be accomplished by using the types anywhere else so nobody do so and it wouldn't be actively harmful to allow them to be used anywhere other types can be used. Only the ability to create values of the type needs to be restricted and we can already write types like that by marking the initializers private.
> > 
> > Any thoughts on this approach?
> 
> This is still a special-case type-checking rule, which means that it's still basically my proposal except, instead of just rewriting the call to use a labeled literal initializer, it rewrites the call to use a magic initializer which cannot be used by users because it requires arguments that users cannot create. I just don't understand the motivation here. It's not a simpler language model to use, explain, or implement; it purports to be conceptually simpler and less magical while actually inventing two magic new language concepts (IntegerLiteral and #literal) on top of the same special cases.

I understand that this is still a special case and also that the implementation is pretty much identical. But I think the way it is presented to users is important.

The motivation is to come up with a design that lets us implement your proposal without magically eliding a parameter label. There is no other case in Swift where you can directly call any function (initializer or otherwise) without including labels for all parameters that have external labels. Creating a special case to allow that here is inconsistent. Why wouldn’t look for a way to do this that treats the signature consistently with the rest of the language and is more in line with how we handle other compiler magic (i.e. #file, #line, etc)?

I am not the only one who feels that way. Quoting Brent:

"But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.”

I agree with this. That is the motivation for my suggestion. I think it’s at least worth discussing as an alternative to magically allowing an external parameter label to be omitted. Brent, what do you think of my suggestion?

> Literals are always going to involve some magic behind the scenes. The non-trivial literals, for example, all actually traffic through some sort of builtin protocol (e.g. BuiltinIntegerLiteralConvertible) which cannot be implemented by users and whose construction necessarily involves a private API contract between the standard library and compiler that we reserve the right to change at will without bringing in swift-evolution. All of the supposedly simple explanations eventually reach, "okay, but if Int isn't a special type, how does the literal turn into an Int in the first place", and that is just unanswerable, because it turns out that standard library types \*are\* actually special in the sense that they get to conform to BuiltinIntegerLiteralConvertible and nobody else does.

Understood. But Swift does a good job of making the magic feel consistent with the rest of the language. That’s a good thing. Allowing omission of a parameter label is a break from that IMO.

I believe this is an important problem to solve but I also think we should try to find a way to solve it that doesn’t violate rules that apply everywhere else in the language (specifically regarding parameter labels).

-Matthew

> **···**
>
> > On Jun 3, 2016, at 1:36 PM, John McCall \<rjmccall@apple.com\> wrote:
> > 
> > > On Jun 2, 2016, at 6:48 PM, Matthew Johnson \<matthew@anandabits.com \<mailto:matthew@anandabits.com\>\> wrote:  
> > > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > > > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com \<mailto:xiaodi.wu@gmail.com\>\> wrote:
> 
> > John.
> > 
> > > > John.
> > > > 
> > > > > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > > > > 
> > > > > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > > > 
> > > > > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > > > > 
> > > > > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > > > > 
> > > > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > > > > 
> > > > > --  
> > > > > Brent Royal-Gordon  
> > > > > Architechies
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 4, 2016, 1:20am UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/53 "2016-06-04T01:20:50Z")

</div>

This is essentially already how it works. The literal protocols are not invoked via  
overload resolution; Swift always invokes the initializer that satisfies the protocol  
requirement.

That is, you can provide ten different init(integerLiteral: T) initializers, but that will  
just prevent the compiler from inferring the associated type, so you'll have to  
declare it. Once you do, that associated type will determine the initializer that  
satisfies the requirement, and that'll always be the initializer chosen.

John.

> **···**
>
> > On Jun 3, 2016, at 5:31 PM, Ben Rimmington via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > John McCall wrote:
> > 
> > > I think that's a very promising way of thinking about literals. Writing  
> > > a literal creates a notional value whose type is the informal, infinite-  
> > > precise type of all integer/FP/collection/etc. literals, which (1) can be  
> > > implicitly converted to any type that implements the appropriate protocol  
> > > and (2) in fact \*must\* be converted to some such type (possibly the  
> > > default type for that literal) in order for the code to be executable.
> > 
> > Could you allow IntegerLiteralConvertible.IntegerLiteralType associatedtype  
> > to override the default Swift.IntegerLiteralType typealias iff there's more  
> > than one unlabelled init(\_:) to choose from? Then you can call the "correct"  
> > init(\_:) instead of calling init(integerLiteral:) as a "special case".

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 4, 2016, 3:11am UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/54 "2016-06-04T03:11:47Z")

</div>

I think your original proposal is good, but just in case:

[Currently]

&nbsp;&nbsp;&nbsp;&nbsp;// Literal `7` is converted to `Swift.IntegerLiteralType` typealias.  
&nbsp;&nbsp;&nbsp;&nbsp;UInt16(7) calls UInt16.init(\_ value: Int)

[Suggested]

&nbsp;&nbsp;&nbsp;&nbsp;// Literal `7` is converted to `UInt16.IntegerLiteralType` associatedtype.  
&nbsp;&nbsp;&nbsp;&nbsp;UInt16(7) calls UInt16.init(\_ value: UInt16)

Have "user-defined literals" already been rejected for Swift?  
\<[C++11 - Wikipedia](https://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals&gt);  
\<[C++14 - Wikipedia](https://en.wikipedia.org/wiki/C%2B%2B14#Standard_user-defined_literals&gt);

-- Ben

> **···**
>
> > On 4 Jun 2016, at 02:20, John McCall \<rjmccall@apple.com\> wrote:
> > 
> > > On Jun 3, 2016, at 5:31 PM, Ben Rimmington via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > John McCall wrote:
> > > 
> > > > I think that's a very promising way of thinking about literals. Writing  
> > > > a literal creates a notional value whose type is the informal, infinite-  
> > > > precise type of all integer/FP/collection/etc. literals, which (1) can be  
> > > > implicitly converted to any type that implements the appropriate protocol  
> > > > and (2) in fact \*must\* be converted to some such type (possibly the  
> > > > default type for that literal) in order for the code to be executable.
> > > 
> > > Could you allow IntegerLiteralConvertible.IntegerLiteralType associatedtype  
> > > to override the default Swift.IntegerLiteralType typealias iff there's more  
> > > than one unlabelled init(\_:) to choose from? Then you can call the "correct"  
> > > init(\_:) instead of calling init(integerLiteral:) as a "special case".
> > 
> > This is essentially already how it works. The literal protocols are not invoked via  
> > overload resolution; Swift always invokes the initializer that satisfies the protocol  
> > requirement.
> > 
> > That is, you can provide ten different init(integerLiteral: T) initializers, but that will  
> > just prevent the compiler from inferring the associated type, so you'll have to  
> > declare it. Once you do, that associated type will determine the initializer that  
> > satisfies the requirement, and that'll always be the initializer chosen.
> > 
> > John.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 8, 2016, 6:20pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/55 "2016-06-08T18:20:43Z")

</div>

Sure thing. I agree that that is an interesting way of conceptualizing how  
this proposal works, at least for value types where the initializer is synthesized:  
you could think of it as saying that there's a rule to prefer the "copy initializer"  
when the argument is a literal and the type is literally constructible.

The flip side is that that argument wouldn't apply to class types or (presumably)  
generic type parameters, but we'd still want e.g. T(1) to work if T: IntegerLiteralConvertible.

John.

> **···**
>
> > On Jun 8, 2016, at 11:15 AM, Dave Abrahams \<dabrahams@apple.com\> wrote:  
> > on Wed Jun 08 2016, John McCall \<rjmccall-AT-apple.com\> wrote:
> > 
> > > > On Jun 7, 2016, at 4:25 PM, Dave Abrahams \<dabrahams@apple.com\> wrote:  
> > > > on Tue Jun 07 2016, John McCall \<rjmccall-AT-apple.com\> wrote:
> > > > 
> > > > > > On Jun 5, 2016, at 5:18 PM, Dave Abrahams via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > > > on Thu Jun 02 2016, John McCall \<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. However, even if you disagree, it's  
> > > > > > > clear that programmers are going to continue to independently try to  
> > > > > > > use it, so it's really unfortunate for it to be subtly wrong.
> > > > > > > 
> > > > > > > Therefore, I propose that we adopt the following typing rule:
> > > > > > > 
> > > > > > > Given a function call expression of the form A(B) (that is, an  
> > > > > > > expr-call with a single, unlabelled argument) where B is an  
> > > > > > > expr-literal or expr-collection, if A has type T.Type for some type T  
> > > > > > > and there is a declared conformance of T to an appropriate literal  
> > > > > > > protocol for B, then the expression is always resolves as a literal  
> > > > > > > construction of type T (as if the expression were written "B as A")  
> > > > > > > rather than as a general initializer call.
> > > > > > > 
> > > > > > > Formally, this would be a special form of the argument conversion  
> > > > > > > constraint, since the type of the expression A may not be immediately  
> > > > > > > known.
> > > > > > 
> > > > > > I realize this is somewhat tangential, but... IMO this may not be entirely  
> > > > > > about literals.
> > > > > > 
> > > > > > We have a standard that full-width type conversions are written as a  
> > > > > > label-free initializer  
> > > > > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion)  
> > > > > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt;&gt;%5C).  
> > > > > > I believe that is partly responsible for setting up the expectation that  
> > > > > > Int(42) works as one would expect. It gets ultra-weird when you can  
> > > > > > convert from type A to type B using B(someA) but you can't write  
> > > > > > B(someB). We should automatically generate a label-free “copy  
> > > > > > initializer” for value types, to complete implementation of the expected  
> > > > > > mental model.
> > > > > 
> > > > > That may also be a good idea, but it won't magically be preferred for  
> > > > > literal construction if the type has any other constructors of  
> > > > > literal-convertible type.
> > > > 
> > > > I know. I'm saying, fixing this for literals without giving value types  
> > > > copy initializers leaves us with only a partial realization of a larger  
> > > > mental model to which I believe people are programming.
> > > 
> > > That's fair. Would you like me to incorporate that into my proposal,  
> > > then? I see the relation, but it's a pretty significant jump in  
> > > scope.
> > 
> > I'm not sure it should be in the same proposal; I just want it to be a  
> > part of the discussion. We should be able to make incremental progress,  
> > but let's make sure we understand how our changes fit into the big picture.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 3, 2016, 8:17pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/56 "2016-06-03T20:17:11Z")

</div>

> > > > > The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.
> > > > 
> > > > I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.
> > > 
> > > It sounds like the reason you don't want to remove the label is because it distinguishes the literal initializer and you don't want it called with a variable accidentally right?
> > > 
> > > What if we do something like this:
> > > 
> > > init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)
> > > 
> > > The idea here is that the IntegerLiteral type and friends are special types that have no members and cannot be used except by the literal convertible protocols and their conformances. There is nothing you can do with the value at all.
> > > 
> > > The trick is that by defaulting it to #literal we are indicating the compiler synthesizes the logical value, as with #file, #line, etc. The compiler would refuse to synthesize this value when a literal is not used. This means it is never selected when the user provides a variable to a converting initializer. If an independent initializer accepting a value of the same type also exists that would be selected. However, when a literal \*is\* used and the type conforms to the relevant literal convertible protocol the compiler always synthesized the value making it always the most specific overload.
> > > 
> > > Of course no runtime value would actually exist. This is just a logical value marking the fact that a literal was used to call the initializer.
> > > 
> > > This approach solves the same problem while retaining semantic consistency with the language (no label elision or short circuit of overload resolution). The magic is arguably a lot more restrained - types for which values can only be supplied by the compiler. We could artificially restrict usage of these types if we wanted to, but we wouldn't have to. Nothing could be accomplished by using the types anywhere else so nobody do so and it wouldn't be actively harmful to allow them to be used anywhere other types can be used. Only the ability to create values of the type needs to be restricted and we can already write types like that by marking the initializers private.
> > > 
> > > Any thoughts on this approach?
> > 
> > This is still a special-case type-checking rule, which means that it's still basically my proposal except, instead of just rewriting the call to use a labeled literal initializer, it rewrites the call to use a magic initializer which cannot be used by users because it requires arguments that users cannot create. I just don't understand the motivation here. It's not a simpler language model to use, explain, or implement; it purports to be conceptually simpler and less magical while actually inventing two magic new language concepts (IntegerLiteral and #literal) on top of the same special cases.
> 
> I understand that this is still a special case and also that the implementation is pretty much identical. But I think the way it is presented to users is important.
> 
> The motivation is to come up with a design that lets us implement your proposal without magically eliding a parameter label. There is no other case in Swift where you can directly call any function (initializer or otherwise) without including labels for all parameters that have external labels.

Don't think of it as omitting a parameter label. Constructing a value from a literal is a complex and well-defined process that happens to end by calling an initializer with a labeled argument. The proposal says that we follow that entire process as normal, not just that we silently rewrite the call to add a label, which is not how it works.

> Creating a special case to allow that here is inconsistent. Why wouldn’t look for a way to do this that treats the signature consistently with the rest of the language and is more in line with how we handle other compiler magic (i.e. #file, #line, etc)?

It's not more consistent with the rest of the language. You're talking about introducing a default argument that only defaults when the compiler recognizes a special syntactic form in the call site. I don't understand why inventing novel and massively invasive language mechanisms like that is somehow clearer or more consistent than explaining that a particular syntactic form is recognized as performing the well-defined literal-construction process.

There is simply no way that this is going to feel 100% consistent with the ordinary language while still prohibiting the literal initializer from being called as an unlabelled initializer in the ordinary language, which is explicitly something we do not want to allow. It will be a special case. It will not be consistent. Special cases are not consistent. You cannot thread this needle.

John.

> **···**
>
> > On Jun 3, 2016, at 12:41 PM, Matthew Johnson \<matthew@anandabits.com\> wrote:
> > 
> > > On Jun 3, 2016, at 1:36 PM, John McCall \<rjmccall@apple.com \<mailto:rjmccall@apple.com\>\> wrote:
> > > 
> > > > On Jun 2, 2016, at 6:48 PM, Matthew Johnson \<matthew@anandabits.com \<mailto:matthew@anandabits.com\>\> wrote:  
> > > > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > > > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com \<mailto:xiaodi.wu@gmail.com\>\> wrote:
> 
> > I am not the only one who feels that way. Quoting Brent:
> > 
> > "But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.”
> > 
> > I agree with this. That is the motivation for my suggestion. I think it’s at least worth discussing as an alternative to magically allowing an external parameter label to be omitted. Brent, what do you think of my suggestion?
> > 
> > > Literals are always going to involve some magic behind the scenes. The non-trivial literals, for example, all actually traffic through some sort of builtin protocol (e.g. BuiltinIntegerLiteralConvertible) which cannot be implemented by users and whose construction necessarily involves a private API contract between the standard library and compiler that we reserve the right to change at will without bringing in swift-evolution. All of the supposedly simple explanations eventually reach, "okay, but if Int isn't a special type, how does the literal turn into an Int in the first place", and that is just unanswerable, because it turns out that standard library types \*are\* actually special in the sense that they get to conform to BuiltinIntegerLiteralConvertible and nobody else does.
> > 
> > Understood. But Swift does a good job of making the magic feel consistent with the rest of the language. That’s a good thing. Allowing omission of a parameter label is a break from that IMO.
> > 
> > I believe this is an important problem to solve but I also think we should try to find a way to solve it that doesn’t violate rules that apply everywhere else in the language (specifically regarding parameter labels).
> > 
> > -Matthew
> > 
> > > John.
> > > 
> > > > > John.
> > > > > 
> > > > > > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > > > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > > > > > 
> > > > > > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > > > > 
> > > > > > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > > > > > 
> > > > > > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > > > > > 
> > > > > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > > > > > 
> > > > > > --  
> > > > > > Brent Royal-Gordon  
> > > > > > Architechies
> > > > > > 
> > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > swift-evolution mailing list  
> > > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [June 3, 2016, 9:05pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/57 "2016-06-03T21:05:12Z")

</div>

> I am not the only one who feels that way. Quoting Brent:
> 
> "But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.”
> 
> I agree with this. That is the motivation for my suggestion. I think it’s at least worth discussing as an alternative to magically allowing an external parameter label to be omitted. Brent, what do you think of my suggestion?

I think it could be simpler:

&nbsp;&nbsp;public struct Literal\<LiteralType\> {  
&nbsp;&nbsp;&nbsp;&nbsp;public let value: LiteralType  
&nbsp;&nbsp;&nbsp;&nbsp;internal init(\_value value: LiteralType)  
&nbsp;&nbsp;}  
&nbsp;&nbsp;  
&nbsp;&nbsp;public protocol IntegerLiteralConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;associatedtype IntegerLiteralType  
&nbsp;&nbsp;&nbsp;&nbsp;init(\_ literal: Literal\<IntegerLiteralType\>)  
&nbsp;&nbsp;}

Only the standard library can create a Literal, which it would do by constructing the IntegerLiteralType with its magic builtin literal stuff and wrapping it. You still have your magic parameter aspect, but without any actual magic, just access control.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 3, 2016, 8:08pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/58 "2016-06-03T20:08:23Z")

</div>

> The IntegerLiteral type idea might be worth exploring. It does seem to  
> provide some additional consistency. For example, wasn't it clarified on  
> this list just recently that literals don't have a type and adopt one based  
> on context? It'd be nice to point out that 42 is an IntegerLiteral when  
> explaining that it's not an Int.
> 
> I think that's a very promising way of thinking about literals. Writing a  
> literal creates a notional value whose type is the informal,  
> infinite-precise type of all integer/FP/collection/etc. literals, which (1)  
> can be implicitly converted to any type that implements the appropriate  
> protocol and (2) in fact \*must\* be converted to some such type (possibly  
> the default type for that literal) in order for the code to be executable.  
> You can then think about this proposal as saying that an explicit  
> conversion from that informal type to a literal-convertible type follows  
> the same path as an implicit conversion would have.
> 
> It sounds like the reason you don't want to remove the label is because it  
> distinguishes the literal initializer and you don't want it called with a  
> variable accidentally right?
> 
> What if we do something like this:
> 
> init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)
> 
> The idea here is that the IntegerLiteral type and friends are special  
> types that have no members and cannot be used except by the literal  
> convertible protocols and their conformances. There is nothing you can do  
> with the value at all.
> 
> The trick is that by defaulting it to #literal we are indicating the  
> compiler synthesizes the logical value, as with #file, #line, etc. The  
> compiler would refuse to synthesize this value when a literal is not used.  
> This means it is never selected when the user provides a variable to a  
> converting initializer. If an independent initializer accepting a value of  
> the same type also exists that would be selected. However, when a literal  
> \*is\* used and the type conforms to the relevant literal convertible  
> protocol the compiler always synthesized the value making it always the  
> most specific overload.
> 
> Of course no runtime value would actually exist. This is just a logical  
> value marking the fact that a literal was used to call the initializer.
> 
> This approach solves the same problem while retaining semantic consistency  
> with the language (no label elision or short circuit of overload  
> resolution). The magic is arguably a lot more restrained - types for which  
> values can only be supplied by the compiler. We could artificially  
> restrict usage of these types if we wanted to, but we wouldn't have to.  
> Nothing could be accomplished by using the types anywhere else so nobody do  
> so and it wouldn't be actively harmful to allow them to be used anywhere  
> other types can be used. Only the ability to create values of the type  
> needs to be restricted and we can already write types like that by marking  
> the initializers private.
> 
> Any thoughts on this approach?
> 
> This is still a special-case type-checking rule, which means that it's  
> still basically my proposal except, instead of just rewriting the call to  
> use a labeled literal initializer, it rewrites the call to use a magic  
> initializer which cannot be used by users because it requires arguments  
> that users cannot create. I just don't understand the motivation here.  
> It's not a simpler language model to use, explain, or implement; it  
> purports to be conceptually simpler and less magical while actually  
> inventing two magic new language concepts (IntegerLiteral and #literal) on  
> top of the same special cases.
> 
> I understand that this is still a special case and also that the  
> implementation is pretty much identical. But I think the way it is  
> presented to users is important.
> 
> The motivation is to come up with a design that lets us implement your  
> proposal without magically eliding a parameter label. There is no other  
> case in Swift where you can directly call any function (initializer or  
> otherwise) without including labels for all parameters that have external  
> labels. Creating a special case to allow that here is inconsistent. Why  
> wouldn’t look for a way to do this that treats the signature consistently  
> with the rest of the language and is more in line with how we handle other  
> compiler magic (i.e. #file, #line, etc)?
> 
> I am not the only one who feels that way. Quoting Brent:
> 
> "But if you're going to call `init(integerLiteral:)` like it's `init(_:)`,  
> I don't think that's a good idea. Parameter labels are supposed to be  
> significant; we don't want to lose that.”
> 
> I agree with this. That is the motivation for my suggestion. I think  
> it’s at least worth discussing as an alternative to magically allowing an  
> external parameter label to be omitted. Brent, what do you think of my  
> suggestion?
> 
> Literals are always going to involve some magic behind the scenes. The  
> non-trivial literals, for example, all actually traffic through some sort  
> of builtin protocol (e.g. BuiltinIntegerLiteralConvertible) which cannot be  
> implemented by users and whose construction necessarily involves a private  
> API contract between the standard library and compiler that we reserve the  
> right to change at will without bringing in swift-evolution. All of the  
> supposedly simple explanations eventually reach, "okay, but if Int isn't a  
> special type, how does the literal turn into an Int in the first place",  
> and that is just unanswerable, because it turns out that standard library  
> types \*are\* actually special in the sense that they get to conform to  
> BuiltinIntegerLiteralConvertible and nobody else does.
> 
> Understood. But Swift does a good job of making the magic feel consistent  
> with the rest of the language. That’s a good thing. Allowing omission of  
> a parameter label is a break from that IMO.
> 
> I believe this is an important problem to solve but I also think we should  
> try to find a way to solve it that doesn’t violate rules that apply  
> everywhere else in the language (specifically regarding parameter labels).

Would it help if we had an attribute or contextual keyword to describe this  
magic? By this I mean, what if we had `init(\_: @literal Int)` or something  
similar? The magic would be isolated to the fact that you'd have to supply  
an integer literal to such an argument, which through behind-the-scenes  
stuff becomes an Int.

> **···**
>
> On Fri, Jun 3, 2016 at 2:41 PM, Matthew Johnson \<matthew@anandabits.com\> wrote:
> 
> > On Jun 3, 2016, at 1:36 PM, John McCall \<rjmccall@apple.com\> wrote:  
> > On Jun 2, 2016, at 6:48 PM, Matthew Johnson \<matthew@anandabits.com\> \> wrote:  
> > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com\> wrote:
> 
> > -Matthew
> > 
> > John.
> > 
> > John.
> > 
> > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > > \> So, you think that this syntax is enticing to new developers who  
> > > naturally think that the feature works the way that I'm proposing it should  
> > > work, and you think that the right solution is to make the syntax illegal  
> > > so that you can more conveniently tell them it doesn't work that way? :)
> > > 
> > > I think the difference between a cast (which merely reinterprets a value  
> > > as a compatible type) and a fullwidth conversion (which creates a similar  
> > > instance of an incompatible type) is very important to understanding how to  
> > > write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > 
> > > \> You can still tell them that it's a struct and you're calling an  
> > > initializer on it; it's just that the initializer chosen is the special  
> > > literal initializer because the argument is a literal.
> > > 
> > > If you're planning to change `IntegerLiteralConvertible` and friends to  
> > > require a fullwidth conversion initializer like `init(\_ value:  
> > > IntegerLiteralType)`, then this is simply an overload resolution rule. In  
> > > that case, I think your proposal is fine.
> > > 
> > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`,  
> > > I don't think that's a good idea. Parameter labels are supposed to be  
> > > significant; we don't want to lose that.
> > > 
> > > --  
> > > Brent Royal-Gordon  
> > > Architechies
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 3, 2016, 9:17pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/59 "2016-06-03T21:17:18Z")

</div>

> > > > > > The IntegerLiteral type idea might be worth exploring. It does seem to provide some additional consistency. For example, wasn't it clarified on this list just recently that literals don't have a type and adopt one based on context? It'd be nice to point out that 42 is an IntegerLiteral when explaining that it's not an Int.
> > > > > 
> > > > > I think that's a very promising way of thinking about literals. Writing a literal creates a notional value whose type is the informal, infinite-precise type of all integer/FP/collection/etc. literals, which (1) can be implicitly converted to any type that implements the appropriate protocol and (2) in fact \*must\* be converted to some such type (possibly the default type for that literal) in order for the code to be executable. You can then think about this proposal as saying that an explicit conversion from that informal type to a literal-convertible type follows the same path as an implicit conversion would have.
> > > > 
> > > > It sounds like the reason you don't want to remove the label is because it distinguishes the literal initializer and you don't want it called with a variable accidentally right?
> > > > 
> > > > What if we do something like this:
> > > > 
> > > > init(\_ value: IntegerLiteralType, literal: IntegerLiteral = #literal)
> > > > 
> > > > The idea here is that the IntegerLiteral type and friends are special types that have no members and cannot be used except by the literal convertible protocols and their conformances. There is nothing you can do with the value at all.
> > > > 
> > > > The trick is that by defaulting it to #literal we are indicating the compiler synthesizes the logical value, as with #file, #line, etc. The compiler would refuse to synthesize this value when a literal is not used. This means it is never selected when the user provides a variable to a converting initializer. If an independent initializer accepting a value of the same type also exists that would be selected. However, when a literal \*is\* used and the type conforms to the relevant literal convertible protocol the compiler always synthesized the value making it always the most specific overload.
> > > > 
> > > > Of course no runtime value would actually exist. This is just a logical value marking the fact that a literal was used to call the initializer.
> > > > 
> > > > This approach solves the same problem while retaining semantic consistency with the language (no label elision or short circuit of overload resolution). The magic is arguably a lot more restrained - types for which values can only be supplied by the compiler. We could artificially restrict usage of these types if we wanted to, but we wouldn't have to. Nothing could be accomplished by using the types anywhere else so nobody do so and it wouldn't be actively harmful to allow them to be used anywhere other types can be used. Only the ability to create values of the type needs to be restricted and we can already write types like that by marking the initializers private.
> > > > 
> > > > Any thoughts on this approach?
> > > 
> > > This is still a special-case type-checking rule, which means that it's still basically my proposal except, instead of just rewriting the call to use a labeled literal initializer, it rewrites the call to use a magic initializer which cannot be used by users because it requires arguments that users cannot create. I just don't understand the motivation here. It's not a simpler language model to use, explain, or implement; it purports to be conceptually simpler and less magical while actually inventing two magic new language concepts (IntegerLiteral and #literal) on top of the same special cases.
> > 
> > I understand that this is still a special case and also that the implementation is pretty much identical. But I think the way it is presented to users is important.
> > 
> > The motivation is to come up with a design that lets us implement your proposal without magically eliding a parameter label. There is no other case in Swift where you can directly call any function (initializer or otherwise) without including labels for all parameters that have external labels.
> 
> Don't think of it as omitting a parameter label. Constructing a value from a literal is a complex and well-defined process that happens to end by calling an initializer with a labeled argument. The proposal says that we follow that entire process as normal, not just that we silently rewrite the call to add a label, which is not how it works.

This is an implementation detail. It is important to the implementation but is inconsistent on the surface, which is what the programmer sees. The fact is that the programmer-visible surface of the language currently treats it as a parameter label. There are \*no\* exceptions to parameter labels and at least some of us would prefer to keep it that way if we can find an acceptable alternative.

> > Creating a special case to allow that here is inconsistent. Why wouldn’t look for a way to do this that treats the signature consistently with the rest of the language and is more in line with how we handle other compiler magic (i.e. #file, #line, etc)?
> 
> It's not more consistent with the rest of the language. You're talking about introducing a default argument that only defaults when the compiler recognizes a special syntactic form in the call site. I don't understand why inventing novel and massively invasive language mechanisms like that is somehow clearer or more consistent than explaining that a particular syntactic form is recognized as performing the well-defined literal-construction process.

I was never fully comfortable but wanted to prompt discussion about some alternative way to mark the initializer that doesn’t contradict the rules for parameter labels. I can see how this compiler generated default would not behave like other compiler generated defaults now and agree it also has problems.

Fortunately it looks like I succeeded in prompting some other alternatives which are better options IMO.

> There is simply no way that this is going to feel 100% consistent with the ordinary language while still prohibiting the literal initializer from being called as an unlabelled initializer in the ordinary language, which is explicitly something we do not want to allow. It will be a special case. It will not be consistent. Special cases are not consistent. You cannot thread this needle.

The way I see it, the question is how to find a way to handle the special case that feels the least inconsistent. Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.

> **···**
>
> > On Jun 3, 2016, at 3:17 PM, John McCall \<rjmccall@apple.com\> wrote:
> > 
> > > On Jun 3, 2016, at 12:41 PM, Matthew Johnson \<matthew@anandabits.com \<mailto:matthew@anandabits.com\>\> wrote:
> > > 
> > > > On Jun 3, 2016, at 1:36 PM, John McCall \<rjmccall@apple.com \<mailto:rjmccall@apple.com\>\> wrote:
> > > > 
> > > > > On Jun 2, 2016, at 6:48 PM, Matthew Johnson \<matthew@anandabits.com \<mailto:matthew@anandabits.com\>\> wrote:  
> > > > > On Jun 2, 2016, at 6:51 PM, John McCall via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > > 
> > > > > > > On Jun 2, 2016, at 4:33 PM, Xiaodi Wu \<xiaodi.wu@gmail.com \<mailto:xiaodi.wu@gmail.com\>\> wrote:
> 
> > John.
> > 
> > > I am not the only one who feels that way. Quoting Brent:
> > > 
> > > "But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.”
> > > 
> > > I agree with this. That is the motivation for my suggestion. I think it’s at least worth discussing as an alternative to magically allowing an external parameter label to be omitted. Brent, what do you think of my suggestion?
> > > 
> > > > Literals are always going to involve some magic behind the scenes. The non-trivial literals, for example, all actually traffic through some sort of builtin protocol (e.g. BuiltinIntegerLiteralConvertible) which cannot be implemented by users and whose construction necessarily involves a private API contract between the standard library and compiler that we reserve the right to change at will without bringing in swift-evolution. All of the supposedly simple explanations eventually reach, "okay, but if Int isn't a special type, how does the literal turn into an Int in the first place", and that is just unanswerable, because it turns out that standard library types \*are\* actually special in the sense that they get to conform to BuiltinIntegerLiteralConvertible and nobody else does.
> > > 
> > > Understood. But Swift does a good job of making the magic feel consistent with the rest of the language. That’s a good thing. Allowing omission of a parameter label is a break from that IMO.
> > > 
> > > I believe this is an important problem to solve but I also think we should try to find a way to solve it that doesn’t violate rules that apply everywhere else in the language (specifically regarding parameter labels).
> > > 
> > > -Matthew
> > > 
> > > > John.
> > > > 
> > > > > > John.
> > > > > > 
> > > > > > > On Thu, Jun 2, 2016 at 18:22 Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > > > > \> So, you think that this syntax is enticing to new developers who naturally think that the feature works the way that I'm proposing it should work, and you think that the right solution is to make the syntax illegal so that you can more conveniently tell them it doesn't work that way? :)
> > > > > > > 
> > > > > > > I think the difference between a cast (which merely reinterprets a value as a compatible type) and a fullwidth conversion (which creates a similar instance of an incompatible type) is very important to understanding how to write Swift, and we shouldn't muddy the waters by creating a magic syntax.
> > > > > > > 
> > > > > > > \> You can still tell them that it's a struct and you're calling an initializer on it; it's just that the initializer chosen is the special literal initializer because the argument is a literal.
> > > > > > > 
> > > > > > > If you're planning to change `IntegerLiteralConvertible` and friends to require a fullwidth conversion initializer like `init(_ value: IntegerLiteralType)`, then this is simply an overload resolution rule. In that case, I think your proposal is fine.
> > > > > > > 
> > > > > > > But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.
> > > > > > > 
> > > > > > > --  
> > > > > > > Brent Royal-Gordon  
> > > > > > > Architechies
> > > > > > > 
> > > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > > swift-evolution mailing list  
> > > > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > > > 
> > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > swift-evolution mailing list  
> > > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 3, 2016, 9:20pm UTC](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861/60 "2016-06-03T21:20:07Z")

</div>

> > I am not the only one who feels that way. Quoting Brent:
> > 
> > "But if you're going to call `init(integerLiteral:)` like it's `init(_:)`, I don't think that's a good idea. Parameter labels are supposed to be significant; we don't want to lose that.”
> > 
> > I agree with this. That is the motivation for my suggestion. I think it’s at least worth discussing as an alternative to magically allowing an external parameter label to be omitted. Brent, what do you think of my suggestion?
> 
> I think it could be simpler:
> 
> &nbsp;&nbsp;public struct Literal\<LiteralType\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;public let value: LiteralType  
> &nbsp;&nbsp;&nbsp;&nbsp;internal init(\_value value: LiteralType)  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;public protocol IntegerLiteralConvertible {  
> &nbsp;&nbsp;&nbsp;&nbsp;associatedtype IntegerLiteralType  
> &nbsp;&nbsp;&nbsp;&nbsp;init(\_ literal: Literal\<IntegerLiteralType\>)  
> &nbsp;&nbsp;}
> 
> Only the standard library can create a Literal, which it would do by constructing the IntegerLiteralType with its magic builtin literal stuff and wrapping it. You still have your magic parameter aspect, but without any actual magic, just access control.

I like this solution as well as Xiaodi’s `@literal` idea better than the idea I came up with. I think I like `@literal` best because it feels more appropriate to restrict an attribute to a specific context (signatures for literal convertible initializers) than anything else. We don’t want to allow this in any other signatures and no types are restricted in that way. The solution I came up with has that problem as well as the `#literal` “value” that is also restricted unlike other compiler generated values.

I think our decision should be based upon which syntactic construct feels least inconsistent with other similar syntactic constructs and therefore feels the least arbitrary. Restricting the usage of a generic `Literal` type to literal convertible initializers feels a little bit less arbitrary than allowing the call site to omit a label, but it feels a little bit more arbitrary than introducing an attribute that has a very specialized context of applicability.

> **···**
>
> > On Jun 3, 2016, at 4:05 PM, Brent Royal-Gordon \<brent@architechies.com\> wrote:
> 
> > --  
> > Brent Royal-Gordon  
> > Architechies

[Previous page](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861.md?page=2)

[Next page](https://forums.swift.org/t/proposal-t-literal-should-construct-t-using-the-appropriate-literal-protocol-if-possible/2861.md?page=4)
