<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I've jotted up a proposal here:<div class=""><br class=""></div><div class=""><a href="https://gist.github.com/charlieMonroe/82e1519dd2b57029f69bc7abe99d7385" class="">https://gist.github.com/charlieMonroe/82e1519dd2b57029f69bc7abe99d7385</a></div><div class=""><br class=""></div><div class="">Please let me know if there are any comments to it.</div><div class=""><br class=""></div><div class="">Charlie<br class="">
<br class=""><div><blockquote type="cite" class=""><div class="">On May 20, 2016, at 9:11 AM, Krystof Vasa via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="">Four questions:</div></div><div class="">1. If I was printing a protocol type that Optional supports, such as Any, would I get a warning?</div></div></blockquote><div class=""><br class=""></div><div class="">This was my bad, I wrote an incorrect example, print("<a href="http://apple.com/(myurl.path)" class="">http://apple.com\(myURL.path)</a>") was what I meant - which will print&nbsp;</div><div class=""><br class=""></div><div class=""><a href="http://apple.comoptional(/iphone/)" class="">http://apple.comOptional(/iphone/)</a>.&nbsp;</div><div class=""><br class=""></div><div class="">Putting an optional directly into print(_:) should be fine with no warning. Only within the string interpolation.</div><br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">2. I believe debugDescription is discouraged from being called directly [from CustomDebugStringConvertible docs]. Perhaps String(reflecting: ) instead, although such debug description behavior could cause different results if you were expecting this fixit to apply to Any types.</div></div></blockquote><div class=""><br class=""></div><div class="">This is not invoked on the value within the Optional, but directly *on* the Optional. As declared here (part of Swift):</div><div class=""><br class=""></div><div class="">extension&nbsp;Optional&nbsp;:&nbsp;CustomDebugStringConvertible&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;/// A textual representation of `self`, suitable for debugging.<br class="">&nbsp; &nbsp;&nbsp;public&nbsp;var&nbsp;debugDescription:&nbsp;String&nbsp;{ get }<br class="">}</div><div class=""><br class=""></div><div class="">Example:</div><div class=""><br class=""></div><div class="">let stringOptional: String? = "Hello"</div><div class=""><br class=""></div><div class="">// Notice no ? is used - the optional is not unwrapped</div><div class="">stringOptional.debugDescription // Optional(Hello)</div><div class=""><br class=""></div><div class="">// Unwrapping the optional</div><div class="">stringOptional!.debugDescription // Hello</div><div class=""><br class=""></div><div class=""><div class="">Not sure what would be the impact of making Optional CustomStringConvertible (i.e. instead of using debugDescription, one would use just description).</div></div><br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">3. How would I have the ability to opt into this behavior for my own types (such as Result or Future)?</div><div class="">4. How would I opt in/out of this behavior for my own&nbsp;StringInterpolationConvertible implementations?</div></div></blockquote><div class=""><br class=""></div><div class="">This is not about customizing the interpolation but about warning the user when using optionals in string interpolation to prevent from such mistakes as above with the URL. This is more common than one would think and sometimes is hard to spot. I'm sorry if I misunderstood you questions.</div><br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">-DW</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">I'm not saying *removing* the current behavior, but adding a warning for this - you'd get the same result ignoring the warning and applying the Fix-It, but you'd have control over this.</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On May 20, 2016, at 6:48 AM, Dan Appel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">&gt;google for swift print optional stackoverflow. I think that kind of speaks for itself.<div class=""><br class=""><div dir="ltr" class=""><div class="">I think this is actually an example of why the current behavior is a<span class="Apple-converted-space">&nbsp;</span><i class="">good</i>&nbsp;thing. I did just google that and the top comment of the first result explains what an optional is. That is very good and encourages beginners to understand how optionals work under the hood. If you hide that from them, they will only be even more confused when they see just the string "nil" pop up when it previously was showing the correct value.</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, May 19, 2016 at 9:36 PM Krystof Vasa via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">BTW - google for swift print optional stackoverflow. I think that kind of speaks for itself.<br class=""><br class="">&gt; On May 19, 2016, at 6:07 PM, Jeremy Pereira via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt; -1<br class="">&gt;<br class="">&gt; This seems to me like crippling string interpolation just because sometimes we make mistakes. 99% of the time, if I interpolate an optional, it’s because I want it that way. I don’t want to have to put up with a warning or write the same boilerplate 99% of the time just to flag up the 1% more easily. Sorry.<br class="">&gt;<br class="">&gt;&gt; On 18 May 2016, at 19:50, Krystof Vasa via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;&gt;<br class="">&gt;&gt; The string interpolation is one of the strong sides of Swift, but also one of its weaknesses.<br class="">&gt;&gt;<br class="">&gt;&gt; It has happened to me more than once that I've used the interpolation with an optional by mistake and the result is then far from the expected result.<br class="">&gt;&gt;<br class="">&gt;&gt; This happened mostly before Swift 2.0's guard expression, but has happened since as well.<br class="">&gt;&gt;<br class="">&gt;&gt; The user will seldomly want to really get the output "Optional(something)", but is almost always expecting just "something". I believe this should be addressed by a warning to force the user to check the expression to prevent unwanted results. If you indeed want the output of an optional, it's almost always better to use the ?? operator and supply a null value placeholder, e.g. "\(myOptional ?? "&lt;&lt;none&gt;&gt;")", or use myOptional.debugDescription - which is a valid expression that will always return a non-optional value to force the current behavior.<br class="">&gt;&gt;<br class="">&gt;&gt; Krystof<br class="">&gt;&gt;<br class="">&gt;&gt; _______________________________________________<br class="">&gt;&gt; swift-evolution mailing list<br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&gt;<br class="">&gt; _______________________________________________<br class="">&gt; swift-evolution mailing list<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote></div></div></div><div dir="ltr" class="">--<span class="Apple-converted-space">&nbsp;</span><br class=""></div><div class=""><div dir="ltr" class=""><div class=""><div class="">Dan Appel<br class=""></div></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><br class="" style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;"><span style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 10px; 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-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>