<div dir="ltr">Thanks for teaching me about .map on Optional. Very cool. <div><br></div><div>I agree with the comment of $! misleading, maybe a better alternative would be &quot;??&quot; ?<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Feb 8, 2017 at 5:12 PM Tony Allevato &lt;<a href="mailto:tony.allevato@gmail.com">tony.allevato@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"><div dir="ltr" class="gmail_msg">What you&#39;re asking for is already possible (avoiding the optional unwrap) by combining map() on Optional with ??:<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">```</div><div class="gmail_msg"><div class="gmail_msg">let name1: String? = &quot;name&quot;</div><div class="gmail_msg">print(name1.map { &quot;\&quot;\($0)\&quot;&quot; } ?? &quot;null&quot;)  // prints &quot;\&quot;name\&quot;&quot;</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">let name2: String? = nil</div><div class="gmail_msg">print(name2.map { &quot;\&quot;\($0)\&quot;&quot; } ?? &quot;null&quot;)  // prints &quot;null&quot;</div><div class="gmail_msg">```</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">So I guess the question is, does simplifying that rise to the level of wanting a custom operator? I personally don&#39;t think it does, but I could see an argument being made that an operator with defined semantics might be a small amount clearer than map + ??. But I think the benefits would have to be very strong, though.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">And as other folks have mentioned, having &quot;!&quot; in the operator name is somewhat misleading, since when I see that I expect a trap to occur in nil cases.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><br class="gmail_msg"><div class="gmail_quote gmail_msg"></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">On Wed, Feb 8, 2017 at 6:04 AM Maxim Veksler via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="white-space:pre-wrap;background-color:rgb(255,254,254)" class="gmail_msg">  </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">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></blockquote></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
_______________________________________________<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">
</blockquote></div></div></div></blockquote></div></div></div>