<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 <<a href="mailto:martinr448@gmail.com">martinr448@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Isn't that exactly what the nil-coalescing operator ?? does?<br class="gmail_msg">
<br class="gmail_msg">
func query(name: String?) -> String {<br class="gmail_msg">
return "{ param: \(name ?? "null") }"<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:'anonymous pro'"><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">?) -> </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:'anonymous pro';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">"{ 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">"null") }"</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro';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:'anonymous pro';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">//=> { param: null }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro';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)">"Max"</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)) </span><span style="font-variant-ligatures:no-common-ligatures">//=> { param: Max }</span></p><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div><div>The output I'm expecting from the second call should be is <span style="color:rgb(0,143,0);font-family:'anonymous pro';font-size:11px">{ param: "Max" }</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 "null" 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'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">
> Am 08.02.2017 um 15:04 schrieb Maxim Veksler via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>>:<br class="gmail_msg">
><br class="gmail_msg">
> Hello,<br class="gmail_msg">
><br 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">
><br class="gmail_msg">
> A (sampled) illustration of the code that currently solves it looks as following:<br class="gmail_msg">
><br class="gmail_msg">
> func query(name: String?) {<br class="gmail_msg">
> let variables_name = name != nil ? "\"\(name!)\"" : "null"<br class="gmail_msg">
> return "{ param: \(variables_name) }"<br class="gmail_msg">
> }<br class="gmail_msg">
><br class="gmail_msg">
> Based on optional status the following output is expected<br class="gmail_msg">
><br class="gmail_msg">
> let name = "Max"<br class="gmail_msg">
> query(name: name)<br class="gmail_msg">
> // { param: "Max" }<br class="gmail_msg">
><br class="gmail_msg">
> let name: String? = nil<br class="gmail_msg">
> query(name: name)<br class="gmail_msg">
> // { param: null }<br class="gmail_msg">
><br class="gmail_msg">
> 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">
><br class="gmail_msg">
> func query(name: String?) {<br class="gmail_msg">
> return "{ param: \(name !? "\"\(name)\"": "null") }"<br class="gmail_msg">
> }<br class="gmail_msg">
><br class="gmail_msg">
> 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">
><br 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.<br class="gmail_msg">
><br class="gmail_msg">
> I'm wondering what the community thinks of the displayed use case, and proposed solution?<br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
> -m<br 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" 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>