<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 2, 2016 at 12:11 PM, Xiaodi Wu <span dir="ltr">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">+1 to the proposal. I can also see the argument for disallowing multiple ways of doing the same thing, though disallowing the use of `as` in this way might be introducing another special case.<div><br></div></div></blockquote><div><br></div><div>The &quot;as&quot;, &quot;as?&quot; and &quot;as!&quot; operators in Swift are already surprisingly overloaded.  Joe Groff&#39;s proposal lists 9 (!!!) different things &quot;as?&quot; does here: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0083-remove-bridging-from-dynamic-casts.md">https://github.com/apple/swift-evolution/blob/master/proposals/0083-remove-bridging-from-dynamic-casts.md</a>.</div><div><br></div><div>&quot;as&quot; is also overloaded in this sense. It performs bridging casts (soon to go away?), upcasts that can never fail (e.g. subclass to superclass), and defining the concrete type of a literal expression. It wouldn&#39;t be a big loss for &quot;as&quot; to lose the last meaning.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>If the proposal is accepted, I&#39;d also advocate for the suggestion in the initial proposal to apply the rule regardless of the number of parentheses, so that `A((B))` behaves the same way as `A(B)`.</div></div></div></blockquote><div><br></div><div>+1. Yes please.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 2, 2016 at 1:55 PM, Austin Zheng via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">I think we should actually go further.<div><br></div><div>If this proposal is accepted, disallow &quot;as&quot; as a way of specifying the type to construct from a literal.</div><div><br></div><div>If this proposal isn&#39;t accepted, disallow using literal values as the argument to one-unlabeled-argument constructors.</div><div><br></div><div>In either case we should disabuse users of the notion that A(literal) is an initializer that behaves exactly the same as other initializers.</div><div><br></div><div>Austin</div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 2, 2016 at 11:46 AM, Austin Zheng <span dir="ltr">&lt;<a href="mailto:austinzheng@gmail.com" target="_blank">austinzheng@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">+1.<div><br></div><div>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.</div><div><br></div><div>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).</div><div><br></div><div>I think the advantage outweighs the disadvantage.</div><div><br></div><div>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 &quot;literal as A&quot;.</div><span><font color="#888888"><div><br></div><div>Austin</div></font></span></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Thu, Jun 2, 2016 at 9:08 AM, John McCall via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div><div><div style="word-wrap:break-word">The official way to build a literal of a specific type is to write the literal in an explicitly-typed context, like so:<div><font face="Andale Mono">    let x: UInt16 = 7</font></div><div>or</div><div><font face="Andale Mono">    let x = 7 as UInt16</font></div><div><br></div><div>Nonetheless, programmers often try the following:</div><div><font face="Andale Mono">    UInt16(7)</font></div><div><br></div><div>Unfortunately, this does <i>not</i> 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.</div><div><br></div><div>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 &quot;as&quot; syntax.  However, even if you disagree, it&#39;s clear that programmers are going to continue to independently try to use it, so it&#39;s really unfortunate for it to be subtly wrong.</div><div><br></div><div>Therefore, I propose that we adopt the following typing rule:</div><div><br></div><div>  Given a function call expression of the form A(B) (that is, an <i>expr-call</i> with a single, unlabelled argument) where B is an <i>expr-literal</i> or <i>expr-collection</i>, 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 &quot;B as A&quot;) rather than as a general initializer call.</div><div><br></div><div>Formally, this would be a special form of the argument conversion constraint, since the type of the expression A may not be immediately known.</div><div><br></div><div>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&#39;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 &quot;A(Int(B))&quot;).</div><div><br></div><div>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&#39;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&lt;Int&gt;([]) or the same with a typealias, but I think there&#39;s some benefit from having a simpler specification, both for the implementation and for the explicability of the model.</div><span><font color="#888888"><div><br></div><div>John.</div></font></span></div><br></div></div><span>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></span></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div></div>