<div dir="ltr"><div>Hello Adrian, </div><div><br></div>Please see reply inline.<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Feb 8, 2017 at 4:52 PM Adrian Zubarev &lt;<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_markdown gmail_msg"><p class="gmail_msg">You could always write something like this:</p>

<pre class="gmail_msg"><code class="m_2050498769850917921swift gmail_msg">func query(name: String?) -&gt; String {
     
    let variables_name = String(format: name == nil ? &quot;%@&quot; : &quot;\&quot;%@\&quot;&quot;, name ?? &quot;null&quot;)
    return &quot;{ param: \(variables_name) }&quot;
}

query(name: &quot;Max&quot;) //=&gt; &quot;{ param: &quot;Max&quot; }&quot;
query(name: nil)   //=&gt; &quot;{ param: null }&quot;
</code></pre>

<p class="gmail_msg">As far as I’m concerned, string interpolation in Swift does not allow nested string literals (yet).</p></div></div></blockquote><div><br></div><div>Thanks, that&#39;s another take on the specifics of String, but I think <b>?!</b> extends beyond String. </div><div><br></div><div>For example, assume that we&#39;re dealing with a callback of some form.</div><div><br></div><div><font face="monospace">let callback: CallbackProtocol? = StructExampleCallback()</font></div><div><br></div><div>You could write inside you handler function:</div><div><br></div><div><font face="monospace">func handler() -&gt; Result? {</font></div><div><font face="monospace">  return callback ?! callback.result() : nil</font></div><div><font face="monospace">}</font></div><div><br></div><div>Which would return either nil, or the callback result. I don&#39;t think you can do it with ?? but then again guard is a valid solution for this specific use case.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_markdown gmail_msg">

<hr class="gmail_msg">

<p class="gmail_msg">But a ternary operator that could potentially unwrap a list of optional values would be neat.</p>

<p class="gmail_msg">Bikeshedding:</p>

<pre class="gmail_msg"><code class="m_2050498769850917921swift gmail_msg">(optional1, optional2, optional1) ? { tupleWithUnwrappedOptionals in return R } : { R }

// variadic generic parameter list
ternary func ?: &lt;…T, R&gt;(list: (…T?), lhs: ((…T) -&gt; R), rhs: () -&gt; R) -&gt; R {
     
    if let unwrappedList = // unwrap all optionals in `list` {
        return lhs(unwrappedList)
    } else {
        return rhs()
    }
}
</code></pre>

<p class="gmail_msg"></p></div><div class="m_2050498769850917921bloop_original_html gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_original_html gmail_msg"><div id="m_2050498769850917921bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto" class="gmail_msg"><br></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_original_html gmail_msg"><div id="m_2050498769850917921bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto" class="gmail_msg"></div> <br class="gmail_msg"> <div id="m_2050498769850917921bloop_sign_1486563969740385024" class="m_2050498769850917921bloop_sign gmail_msg"><div style="font-family:helvetica,arial;font-size:13px" class="gmail_msg">-- <br class="gmail_msg">Adrian Zubarev<br class="gmail_msg">Sent with Airmail</div></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_original_html gmail_msg"> <br class="gmail_msg"><p class="m_2050498769850917921airmail_on gmail_msg">Am 8. Februar 2017 um 15:04:44, Maxim Veksler via swift-evolution (<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>) schrieb:</p> </div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_original_html gmail_msg"><blockquote type="cite" class="m_2050498769850917921clean_bq gmail_msg"><span class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">





<div dir="ltr" class="gmail_msg">Hello, 
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">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"></div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">A (sampled) illustration of the code that currently solves it
looks as following:</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">
<div class="gmail_msg"><font face="monospace" class="gmail_msg">func query(name: String?)
{</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">  <span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">let
variables_name = name != nil ? &quot;\&quot;\(name!)\&quot;&quot; :
&quot;null&quot;</span></font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg"><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">
return &quot;{ param: \(</span><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">variables_name</span><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">)
}&quot;</span></font></div>
<div class="gmail_msg"><span style="font-family:monospace;color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">
}</span><br class="gmail_msg"></div>
</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">Based on optional status the following output is
expected</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">let name = &quot;Max&quot;</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">query(name: name) </font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">// { param: &quot;Max&quot; }</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg"><br class="gmail_msg"></font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">let name: String? = nil</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">query(name: name)</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">// { param: null }</font></div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">I think it might be useful to have an conditional unwrap
operator <font face="monospace" class="gmail_msg">!?</font> that would enable syntax
sugar uch as the following built into the language:</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">func query(name: String?)
{</font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg">  <span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">return
&quot;{ param: \(name !? &quot;\&quot;\(name)\&quot;&quot;: &quot;null&quot;) }&quot;</span></font></div>
<div class="gmail_msg"><font face="monospace" class="gmail_msg"><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">
}</span><br class="gmail_msg"></font></div>
<div class="gmail_msg"><span style="color:rgb(5,0,0);font-family:&#39;andale mono&#39;,&#39;lucida console&#39;,monospace;font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">
<br class="gmail_msg"></span></div>
<div class="gmail_msg"><span style="background-color:rgb(255,254,254)" class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg">This expression is
expected to produce same output as the examples above. It means
check the Optional state of</span></font></span> <span style="font-family:monospace" class="gmail_msg">name: String?</span><span style="background-color:rgb(255,254,254)" class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg">, 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 </span></font></span><span style="color:rgb(5,0,0);white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">false</span>
<span style="white-space:pre-wrap;color:rgb(5,0,0);background-color:rgb(255,254,254)" class="gmail_msg">
condition part of the expression.</span></div>
<div class="gmail_msg"><span style="background-color:rgb(255,254,254)" class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg"><br class="gmail_msg"></span></font></span></div>
<div class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="background-color:rgb(255,254,254)" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg">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.</span></span></font></div>
<div class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="background-color:rgb(255,254,254)" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg"><br class="gmail_msg"></span></span></font></div>
<div class="gmail_msg">
<div class="gmail_msg">I&#39;m wondering what the community thinks of the displayed use
case, and proposed solution?</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg"><font color="#050000" class="gmail_msg"><span style="background-color:rgb(255,254,254)" class="gmail_msg"><span style="white-space:pre-wrap" class="gmail_msg">-m</span></span></font></div>
</div></div></div></span></blockquote></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_2050498769850917921bloop_original_html gmail_msg"><blockquote type="cite" class="m_2050498769850917921clean_bq gmail_msg"><span class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">


_______________________________________________<br class="gmail_msg">swift-evolution mailing list<br class="gmail_msg"><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"></div></div></span></blockquote></div></div></blockquote></div></div>