<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="direction: inherit;">It's an interesting idea that needs to be written down in a separate proposal and is tangentially related to the problem we are trying to solve here and now. It is trivial to define this operator and was suggested by Charlie as new API to be added to Optional the last time improving Optionals in interpolation segments was discussed. For now we're trying to keep it light and bugfix-esque.</div><br>~Robert Widmann</div><div><br>2016/10/04 13:29、Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> のメッセージ:<br><br></div><blockquote type="cite"><div>
<title></title>
<div>On Tue, Oct 4, 2016, at 10:28 AM, Nate Cook wrote:<br></div>
<blockquote type="cite"><div><blockquote type="cite"><div>On Oct 3, 2016, at 5:49 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div>
<div><br></div>
<div><div><div>On Mon, Oct 3, 2016, at 03:18 PM, Jordan Rose wrote:<br></div>
<blockquote type="cite"><div><blockquote type="cite"><div><br></div>
</blockquote></div>
</blockquote></div>
</div>
</blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">...<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><div><div><blockquote type="cite"><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<String>. 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"></span><br></div>
<div><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"> print("x: \(x ?? "nil")")</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></div>
<div><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"> ~ ^ ~~~~~</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></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 -> T), (T?, @autoclosure () thro</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></div>
<div><span class="font" style="font-family:menlo, consolas, monospace, sans-serif">ws -> T?)</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></div>
<div><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"> print("x: \(x ?? "nil")")</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></div>
<div><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"> ^</span><span class="font" style="font-family:menlo, consolas, monospace, sans-serif"></span><br></div>
<div>This leads to writing code like "… \(x.map(String.init(describing:)) ?? "nil")" which is pretty gross.<br></div>
</div>
</div>
</blockquote><div><br></div>
</div>
<div>I think that if we're going to add this warning we should make it possible to provide a string as an alternative. It seems like it should be possible to build a <span class="font" style="font-family:Menlo">??</span> operator with a <span class="font" style="font-family:Menlo">(T?, String) -> _StringInterpolationSomething</span> signature that works only in a string interpolation context.<br></div>
<div><br></div>
<div>There are some types that aren't trivially constructible, or don't have clear alternatives for the nil case. Other times it might just not make sense to build a new instance simply to turn it into a string. If we're going to make people provide an alternative for optionals in this otherwise simple-to-use construct, let's make it simple to do so.<br></div>
<div><br></div>
<div>This is undoubtedly a more complex approach that could be considered separately, but I think it would be a valuable part of how developers could transition their code.<br></div>
</blockquote><div><br></div>
<div>I like this idea. This combined with the warning for naively interpolating an Optional would be a good solution, because now when I see the warning I can trivially solve it with `?? "nil"`.<br></div>
<div><br></div>
<div>-Kevin Ballard</div>
<div><br></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>