<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I'm a bit undecided, as it seems like it doesn't add enough to warrant another operator to learn (and one that's a bit confusing in its purpose, since it doesn't trap like other exclamation mark operators do).</div><div class=""><br class=""></div><div class="">For the specific example you can already just do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func query(name:String?) -> String { return "{ param: \"" + (name ?? "null") + "\" }" }</font></div><div class=""><br class=""></div><div class="">In the case where the input value isn't an optional string you can still do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func myFunc(foo:Foo?) -> String { return foo?.description ?? "null" }</font></div><div class=""><br class=""></div><div class="">I dunno, I'm just not sure having another ternary adds quite enough to justify it, and like I say it's a bit of an odd operator since it doesn't actually trap. For the specific case of a String a more useful alternative might be the ability to put the ?? operator inside a string, with the compiler knowing the final type must be a String (so you can mix types as long as they're CustomStringConvertible), like so:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func query(name:String?) -> String { return "{ param: \"\(name ?? "null")\"" }</font></div><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Feb 2017, at 14:04, Maxim Veksler via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello, <div class=""><br class=""></div><div class="">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=""></div><div class=""><br class=""></div><div class="">A (sampled) illustration of the code that currently solves it looks as following:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace" class="">func query(name: String?) {</font></div><div class=""><font face="monospace" class=""> <span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="">let variables_name = name != nil ? "\"\(name!)\"" : "null"</span></font></div><div class=""><font face="monospace" class=""><span style="white-space:pre-wrap;background-color:rgb(255,254,254)" class=""> </span><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="">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="">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="">) }"</span></font></div><div class=""><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="">}</span><br class=""></div></div><div class=""><br class=""></div><div class="">Based on optional status the following output is expected</div><div class=""><br class=""></div><div class=""><font face="monospace" class="">let name = "Max"</font></div><div class=""><font face="monospace" class="">query(name: name) </font></div><div class=""><font face="monospace" class="">// { param: "Max" }</font></div><div class=""><font face="monospace" class=""><br class=""></font></div><div class=""><font face="monospace" class="">let name: String? = nil</font></div><div class=""><font face="monospace" class="">query(name: name)</font></div><div class=""><font face="monospace" class="">// { param: null }</font></div><div class=""><br class=""></div><div class="">I think it might be useful to have an conditional unwrap operator <font face="monospace" class="">!?</font> that would enable syntax sugar uch as the following built into the language:</div><div class=""><br class=""></div><div class=""><font face="monospace" class="">func query(name: String?) {</font></div><div class=""><font face="monospace" class=""> <span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="">return "{ param: \(name !? "\"\(name)\"": "null") }"</span></font></div><div class=""><font face="monospace" class=""><span style="color:rgb(5,0,0);font-size:9.75px;white-space:pre-wrap;background-color:rgb(255,254,254)" class="">}</span><br class=""></font></div><div class=""><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=""><br class=""></span></div><div class=""><span style="background-color:rgb(255,254,254)" class=""><font color="#050000" class=""><span style="white-space:pre-wrap" class="">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="">name: String?</span><span style="background-color:rgb(255,254,254)" class=""><font color="#050000" class=""><span style="white-space:pre-wrap" class="">, 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="">false </span><span style="white-space:pre-wrap;color:rgb(5,0,0);background-color:rgb(255,254,254)" class="">condition part of the expression.</span></div><div class=""><span style="background-color:rgb(255,254,254)" class=""><font color="#050000" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></font></span></div><div class=""><font color="#050000" class=""><span style="background-color:rgb(255,254,254)" class=""><span style="white-space:pre-wrap" class="">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=""><font color="#050000" class=""><span style="background-color:rgb(255,254,254)" class=""><span style="white-space:pre-wrap" class=""><br class=""></span></span></font></div><div class=""><div class="">I'm wondering what the community thinks of the displayed use case, and proposed solution?</div></div></div></div></blockquote></div><br class=""></body></html>