<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Mon, Oct 3, 2016, at 03:18 PM, Jordan Rose wrote:<br></div>
<blockquote type="cite"><div><br></div>
<div><blockquote type="cite"><div>On Oct 3, 2016, at 14:41, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div><br></div>
<div><div><div>On Mon, Oct 3, 2016, at 10:52 AM, Harlan Haskins via swift-evolution wrote:<span></span><br></div>
<blockquote type="cite"><div><div><p style="margin-top:0px;margin-bottom:16px;"><span class="highlight" style="background-color:rgba(255, 255, 255, 0)">Swift developers frequently use string interpolation as a convenient, concise syntax for interweaving variable values with strings. The interpolation machinery, however, has surprising behavior in one specific case:&nbsp;<code style="padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">Optional&lt;T&gt;</code>. If a user puts an optional value into a string interpolation segment, it will insert either&nbsp;<code style="padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">"Optional("value")"</code>&nbsp;or&nbsp;<code style="padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;">"nil"</code>&nbsp;in the resulting string. Neither of these is particularly desirable, so we propose a warning and fix-it to surface solutions to these potential mistakes.</span><br></p></div>
</div>
</blockquote><div><br></div>
<div>Is there any way we could instead allow Optionals but just print them the way we print ImplicitlyUnwrappedOptionals? That's almost always how I want my Optionals to work when interpolating. To be specific, this means for .some(x) we just print x, and for .none we print "nil".<br></div>
</div>
</div>
</blockquote></div>
<div>We had this at one point, but we took it out because people would forget to test the nil case. I think `?? ""` or `?? nil` really is the best answer here.<br></div>
</blockquote><div><br></div>
<div>But you can't write that, unless you're dealing specifically with an Optional&lt;String&gt;. &nbsp;If you try you'll get an error:<br></div>
<div><br></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">unnamed.swift:2:19: error: binary operator '??' cannot be applied to operands of type 'Int?' and 'String'</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; print("x: \(x ?? "nil")")</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~ ^&nbsp; ~~~~~</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">unnamed.swift:2:19: note: overloads for '??' exist with these partially matching parameter lists: (T?, @autoclosure () throws -&gt; T), (T?, @autoclosure () thro</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">ws -&gt; T?)</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; print("x: \(x ?? "nil")")</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ^</span><span class="font" style="font-family: menlo, consolas, monospace, sans-serif;"><br></span></div>
<div>This leads to writing code like "… \(x.map(String.init(describing:)) ?? "nil")" which is pretty gross.<br></div>
<div><br></div>
<div>-Kevin<br></div>
</body>
</html>