<div dir="ltr">Hi Martin, please see reply inline.<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Feb 8, 2017 at 4:47 PM Martin R &lt;<a href="mailto:martinr448@gmail.com">martinr448@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Isn&#39;t that exactly what the nil-coalescing operator ?? does?<br class="gmail_msg">
<br class="gmail_msg">
    func query(name: String?) -&gt; String {<br class="gmail_msg">
        return &quot;{ param: \(name ?? &quot;null&quot;) }&quot;<br class="gmail_msg">
    }<br class="gmail_msg">
<br class="gmail_msg"></blockquote><div><br></div><div>No, because</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">func</span><span style="font-variant-ligatures:no-common-ligatures"> query(name: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures">?) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;;color:rgb(180,38,26)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">return</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">&quot;{ param: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">\</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">name ?? </span><span style="font-variant-ligatures:no-common-ligatures">&quot;null&quot;) }&quot;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;;color:rgb(0,143,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">query</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(name: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">nil</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)) </span><span style="font-variant-ligatures:no-common-ligatures">//=&gt; { param: null }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;anonymous pro&#39;;color:rgb(0,143,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">query</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(name: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)">&quot;Max&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)) </span><span style="font-variant-ligatures:no-common-ligatures">//=&gt; { param: Max }</span></p><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div><div>The output I&#39;m expecting from the second call should be is <span style="color:rgb(0,143,0);font-family:&#39;anonymous pro&#39;;font-size:11px">{ param: &quot;Max&quot; }</span></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
If you use the JSONSerialization class from the Foundation library then `nil` is bridged to `NSNull` and translated to &quot;null&quot; automatically (see SE-0140).<br class="gmail_msg">
<br class="gmail_msg"></blockquote><div><br></div><div> I agree that in this case JSONSerialization might be a fitting solution, I also agree that any required output can be solved by a Utility class. </div><div><br></div><div>What I would like to argue is that there is a place for such syntactic sugar because it simplifies readability, for ex. what if I&#39;ve want to wrap my non optional name parameter using %20%, and co. </div><div><br></div><div>JSONSerialization is specific solution, I think that there is a generic case to have in the language some syntactic sugar for a 1 liner composition of unwrap and ternary conditional operation. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Regards,<br class="gmail_msg">
Martin<br class="gmail_msg">
<br class="gmail_msg">
&gt; Am 08.02.2017 um 15:04 schrieb Maxim Veksler via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt;:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Hello,<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Let&#39;s assume I have an optional &quot;name&quot; parameter, based on the optional status of the parameters I would like to compose string with either the unwrapped value of name, or the string &quot;null&quot;. The use case for is syntactic sugar to compose a GraphQL queries.<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; A (sampled) illustration of the code that currently solves it looks as following:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; func query(name: String?) {<br class="gmail_msg">
&gt;   let variables_name = name != nil ? &quot;\&quot;\(name!)\&quot;&quot; : &quot;null&quot;<br class="gmail_msg">
&gt;   return &quot;{ param: \(variables_name) }&quot;<br class="gmail_msg">
&gt; }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Based on optional status the following output is expected<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; let name = &quot;Max&quot;<br class="gmail_msg">
&gt; query(name: name)<br class="gmail_msg">
&gt; // { param: &quot;Max&quot; }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; let name: String? = nil<br class="gmail_msg">
&gt; query(name: name)<br class="gmail_msg">
&gt; // { param: null }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; I think it might be useful to have an conditional unwrap operator !? that would enable syntax sugar uch as the following built into the language:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; func query(name: String?) {<br class="gmail_msg">
&gt;   return &quot;{ param: \(name !? &quot;\&quot;\(name)\&quot;&quot;: &quot;null&quot;) }&quot;<br class="gmail_msg">
&gt; }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; This expression is expected to produce same output as the examples above. It means check the Optional state of name: String?, in case it has a value, unwrap it and make the unwrapped value accessible under the same name to the true condition part of the expression, otherwise return the false condition part of the expression.<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; The effectively removes the need to have the &quot;if != nil&quot; and the forced unwrap syntactical code, and IMHO improves code readability and expressiveness.<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; I&#39;m wondering what the community thinks of the displayed use case, and proposed solution?<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; -m<br class="gmail_msg">
&gt; _______________________________________________<br class="gmail_msg">
&gt; swift-evolution mailing list<br class="gmail_msg">
&gt; <a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
<br class="gmail_msg">
</blockquote></div></div>