<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Definitely right about that,&nbsp;<a href="https://github.com/CodaFi/swift/commit/43a582ef0fb108369f3df955f617b77b71cbd45e#diff-9124ea1eb6021a3c7323d7a07fd85c1eR2409" class="">the implementation</a>&nbsp;took all of 10 minutes after extending the&nbsp;OptionalToAnyCoercionWalker. &nbsp;If you’ve got any comments, please let me know there.<br class=""><br class="">~Robert Widmann<br class=""><br class=""><blockquote type="cite" class="">On Oct 3, 2016, at 4:06 PM, Mark Lacey &lt;<a href="mailto:mark.lacey@apple.com" class="">mark.lacey@apple.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><br class="">On Oct 3, 2016, at 11:26 AM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Oct 3, 2016, at 11:02 AM, Robert Widmann &lt;devteam.codafi@gmail.com&gt; wrote:<br class=""><br class="">Because the initializer here doesn’t take Any, it takes &lt;T&gt;.<br class=""></blockquote><br class="">I think there's a case to be made to generalize the 'Any' warning to Optional implicitly being deduced as a type variable binding in any unconstrained context. What&nbsp;exactly constitutes 'implicit' and 'unconstrained' is up for debate, though, and probably needs some experimentation to figure out what feels good. For instance,&nbsp;explicitly constructing an optional is a signal the optionality intentional. Potentially, having multiple Optional parameters binding the same type variable also&nbsp;increases the likelihood it's intended, for example:<br class=""><br class="">func foo&lt;T&gt;(x: T, y: T) {}<br class=""><br class="">var x: Int? = 1<br class="">var y: Int = 2<br class="">foo(x, y) // One Optional isn't unwrapped, forcing the other to promote. Maybe a mistake?<br class="">var z: Int? = 3<br class="">foo(x, z) // Two T parameters are Optional. Probably intentional?<br class=""><br class="">Regardless of whether there's a more general principle we can base a warning on, string interpolation and String(describing:) are common enough pitfalls that they&nbsp;may just deserve special case treatment.<br class=""></blockquote><br class="">I think string interpolation could be handled pretty easily with a warning by extending the existing warning for Any. We just need to look at interpolation expressions&nbsp;and if any of the segments are optional-typed emit a warning unless they are explicitly casted to the optional type. The fixit can suggest explicit casting or using the&nbsp;debugDescription.<br class=""><br class="">I’m not sure we really need an evolution proposal for that.<br class=""><br class="">As for the more general topic of trickiness around optional injection into unconstrained generics: Yes, we should review that at some point as well. I recall seeing at&nbsp;least one concrete complaint about surprising behavior resulting from doing this in generic functions, but I cannot find the bug at the moment.<br class=""><br class="">Mark<br class=""><br class=""><br class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class="">~Robert Widmann<br class=""><br class=""><blockquote type="cite" class="">On Oct 3, 2016, at 2:00 PM, Harlan Haskins via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Unfortunately, Optional-to-Any does not currently hit this case because IIRC it doesn't promote to Any in an interpolation segment. I tested this with a ToT build&nbsp;yesterday.<br class=""><br class="">- Harlan<br class=""><br class="">On Oct 3, 2016, at 1:57 PM, Joe Groff &lt;jgroff@apple.com&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">We now emit a warning whenever an optional is used as an Any. I disagree that this should be an error, but it seems reasonable to warn (if we don't already&nbsp;thanks to the 'Any' warning).<br class=""><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class="">On Oct 3, 2016, at 10:52 AM, Harlan Haskins via swift-evolution &lt;swift-evolution@swift.org&gt; wrote:<br class=""><br class="">Hey all,<br class=""><br class="">Julio Carrettoni, Robert Widmann, and I have been working on a proposal to mitigate something that's burned us all since Swift 1. We'd love some&nbsp;feedback!<br class=""><br class="">It's available here:&nbsp;https://gist.github.com/harlanhaskins/63b7343e7fe4e5f4c6cfbe9413a98fdd<br class=""><br class="">I've posted the current draft below.<br class=""><br class="">Thanks,<br class="">Harlan Haskins<br class=""><br class="">Disallow Optionals in String Interpolation Segments<br class=""><br class=""><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• Proposal:&nbsp;SE-NNNN<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• Authors:&nbsp;Harlan Haskins,&nbsp;Julio Carrettoni,&nbsp;Robert Widmann<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• Review Manager: TBD<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• Status:&nbsp;Awaiting revie<br class=""></div>Introduction<br class=""><br class="">Swift developers frequently use string interpolation as a convenient, concise syntax for interweaving variable values with strings. The interpolation&nbsp;machinery, however, has surprising behavior in one specific case:&nbsp;Optional&lt;T&gt;. If a user puts an optional value into a string interpolation segment, it will&nbsp;insert either&nbsp;"Optional("value")"&nbsp;or&nbsp;"nil"&nbsp;in the resulting string. Neither of these is particularly desirable, so we propose a warning and fix-it to&nbsp;surface solutions to these potential mistakes.<br class=""><br class="">Swift-evolution thread:&nbsp;Discussion thread topic for that proposal<br class=""><br class="">Motivation<br class=""><br class="">The Swift Programming Language&nbsp;defines string interpolation segments as "a way to construct a new String value from a mix of constants, variables,&nbsp;literals, and expressions". There is one type that runs counter to this definition:&nbsp;Optional. The&nbsp;.none&nbsp;case in particular is used to indicate the absence of&nbsp;a value. Moreover, its inclusion in interpolation segments leads to the dreaded&nbsp;"nil"&nbsp;in output that is often fed to UI elements. Even barring that,&nbsp;interpolating a non-nil optional value yields&nbsp;"Optional("value")", a result that is not useful even in logged output.<br class=""><br class="">Given that the&nbsp;Optional&nbsp;type is never fit for display to the end user, and can often be a surprising find in the console, we propose that requesting an&nbsp;Optional's debug description be an explicit act. This proposal now requires a warning when using an expression of&nbsp;Optional&nbsp;type within a string&nbsp;interpolation segment.<br class=""><br class="">Proposed solution<br class=""><br class="">The user will be warned after attempting to use an expression with type&nbsp;Optional&lt;T&gt;&nbsp;in a string interpolation segment. They will then be offered a fixit&nbsp;suggesting they explicitly request the&nbsp;debugDescription&nbsp;of the&nbsp;Optional&nbsp;value instead.<br class=""><br class="">Detailed design<br class=""><br class="">Semantic analysis currently does not do much but guarantee the well-formedness of expressions in interpolation segments. These are then fed directly to&nbsp;String.init(stringInterpolationSegment:)&nbsp;and are run through the runtime reflection system to generate a description. Semantic analysis will&nbsp;be tweaked to inspect the result of solving an interpolation segment for an&nbsp;Optional&nbsp;and will offer a fixit in that case.<br class=""><br class="">Impact on existing code<br class=""><br class="">As this is a warning, code written before this proposal will continue to compile and run with the same semantics as before. Authors of code that makes use&nbsp;of this unsafe pattern will be offered a migration path to the safer, more explicit form.<br class=""><br class="">Alternatives considered<br class=""><br class=""><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• A fixit that suggests a default value be inserted would be entirely appropriate (following the style of the fixit introduced in&nbsp;SE-0140).<br class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• Forbidding this pattern by hard error would make this proposal a breaking change that is out of scope for this stage of Swift's development.<br class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>• A fixit that introduces a force-unwrapping would technically work as well, however it would be fixing a dangerous operation with yet another dangerous&nbsp;operation.<br class=""><br class=""></div><br class=""><br class="">Sent from my iPad<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""></blockquote>_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote></blockquote><br class=""></body></html>