<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 <<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>> 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?) -> String {
let variables_name = String(format: name == nil ? "%@" : "\"%@\"", name ?? "null")
return "{ param: \(variables_name) }"
}
query(name: "Max") //=> "{ param: "Max" }"
query(name: nil) //=> "{ param: null }"
</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'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'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() -> 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'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 ?: <…T, R>(list: (…T?), lhs: ((…T) -> R), rhs: () -> R) -> 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's assume I have an optional "name" 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 "null".
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 ? "\"\(name!)\"" :
"null"</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 "{ 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">)
}"</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 = "Max"</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: "Max" }</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
"{ param: \(name !? "\"\(name)\"": "null") }"</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:'andale mono','lucida console',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
"if != nil" 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'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>