<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 16, 2015, at 8:19 AM, ilya &lt;<a href="mailto:ilya.nikokoshev@gmail.com" class="">ilya.nikokoshev@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">-1: a big point of ?? operator is the fact that it strips an optional <br class="">Also it's really unclear whether didSet is triggered in the "do nothing" case. <br class=""></div></blockquote><div><br class=""></div><div>It seems the appropriate thing to do in the “do nothing” case is… nothing.&nbsp;</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Dec 16, 2015 at 18:07 Ian Ynda-Hummel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class=""><span style="line-height:1.5" class="">It seems to be that the object property case (which is the most common use of ||= I've seen with Ruby) is mostly solved by the fact that Swift allows you to specify default (or lazy loaded!) values on variable declaration.</span><br class=""></div><div class=""><span style="line-height:1.5" class=""><br class=""></span></div><div class=""><div class=""><span style="line-height:19.5px" class="">&nbsp; &nbsp; class MessagesViewController: UIViewController {</span></div><div class=""><span style="line-height:19.5px" class="">&nbsp; &nbsp; &nbsp; &nbsp; var chatTitleName = "Default"</span></div><div class=""><span style="line-height:19.5px" class="">&nbsp; &nbsp; }</span></div><div style="line-height:1.5" class=""><br class=""></div></div><div class="">As for the case Jacob mentioned above:</div></div><div dir="ltr" class=""><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; var value = someInitialValue</div><div class="">&nbsp; &nbsp; value ??= fallback1</div><div class="">&nbsp; &nbsp; value ??= fallback2</div></div><div class=""><br class=""></div></div><div dir="ltr" class=""><div class="">It seems like that can be written as:</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; var value = someInitialValue</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; ?? fallback1</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; ?? fallback2</div></div><div class=""><br class=""></div><div class="">Which doesn't seem appreciably different to me, though I am curious what others think about the style. That said, I can imagine more complex cases where it might seem appropriate. Say:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; var value = someInitialValue</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; var something</div><div class="">&nbsp; &nbsp; ...do some processing</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; value ??= something</div><div class="">&nbsp; &nbsp; guard value else { return }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; var somethingElse</div><div class="">&nbsp; &nbsp; ...do some processing</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; value ??= somethingElse</div><div class="">&nbsp; &nbsp; guard value else { return }</div><div class=""><br class=""></div><div class="">But that can also be rewritten as something like:</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; var value = someInitialValue</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; ?? {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var something</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...do some processing</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return something</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; ?? {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var somethingElse</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...do some processing</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return somethingElse</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }()</div></div><div class=""><br class=""></div>Again, curious what people think about the style.</div><div dir="ltr" class=""><div class=""><div class=""><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Dec 16, 2015 at 9:28 AM Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">I'm still uncertain whether that would solve the issue at the right location.<div class=""><br class=""></div><div class="">How do you end up in such a situation where this is actually necessary?</div><div class="">Why do I not end up in such situations?</div><div class="">I'd like to understand where the discrepancy comes from.</div><div class=""><br class=""></div><div class="">I.e. where do you define chatTitleName? Where else do you set chatTitleName?</div><div class="">Why isn't it initially set to "Default" an then overwritten on-demand?</div>















</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:22 PM, Kevin Wooten <span dir="ltr" class="">&lt;<a href="mailto:kdubb@me.com" target="_blank" class="">kdubb@me.com</a>&gt;</span> wrote:<br class=""><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=""><br class=""><div class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 16, 2015, at 4:12 AM, Al Skipp via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div style="word-wrap:break-word" class=""><div class=""><blockquote type="cite" class=""><div class="">On 16 Dec 2015, at 00:58, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">I tend towards -1 for multiple reasons:<div class=""><ul class=""><li class="">It has little value for local variables. In most cases you want to use the value you assign to a local variable and assigning it to an optional variable would require a subsequent unwrapping. In most cases where local variables are involved "var x = y ?? z" is satisfying as it creates a non-optional value iff z is non-optional.<br class=""><br class=""></li><li class="">It seems to be a rare use case that you set a value of an optional property which is currently nil and without also using that value directly within the same context. Quickly checking my Swift apps reveals only very little such use cases.<br class=""><br class=""></li><li class="">The remaining cases could expressed like "object.property = object.property ?? …" or using "if object.property == nil { … }".<br class="">While it is true that variable and property name could be very long, this is an unlikely case of an already rare case which decreases the value of the proposed assignment operator even further.<br class=""><br class=""></li><li class="">Most important though is that such an optional assignment operator would work differently from all other assignment operators. The right operand would never be executed if the variable being assigned is already non-nil. This will likely be unexpected for a lot of developers who expect similar behavior like in all other assignments.</li></ul></div></div></div></blockquote>I think these are all very good points. Seems like the only really practical use would be restricted to:</div><div class="">object.property ??= val</div><div class=""><br class=""></div><div class="">Instead of:</div><div class="">object.property = object.property ?? val</div><div class=""><br class=""></div><div class="">Is it worth it for that one scenario? As Marc pointed out, the ?? operator is much more versatile as it can also be used to return a non-optional value.</div></div></div></blockquote><div class=""><br class=""></div></div></div><div class="">After perusing our Swift code it turns out that we use the long form (a = a ?? def) quite a bit.&nbsp; As it was previously mentioned it, when the variables is named “a” it’s clearly not an issue, but this is…</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; messagesViewController.chatTitleName = &nbsp;messagesViewController.chatTitleName ?? “Default”</div><div class=""><br class=""></div><div class="">(Those are effectively real world variable names).&nbsp;</div><div class=""><br class=""></div><div class="">I think quite a bit of the clarity of this statement is lost by the duplication and the proposed form..</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; messagesViewController.chatTitleName ??= “Default”</div><div class=""><br class=""></div><div class="">clears it up fairly well.</div><span class=""><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap:break-word" class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1Ry8ov4zuU66O51YNcjYWV1Hx5tIf84hnBKbccmE4N33zqePejzhJC1Riz7ZtUMNSSg9xuL-2BVThDuxITUjvWcpLmJP1b8Od93jgjWBuLmj56-2FyavCIM1YMC4twcMHHoYU00T4ubpfnmIWcXyz76iNZ8YRaMgNfS044SFTr08aDhHHenb-2B3UoRzwXH8GKd-2B9i8wYZ3imfwe7xP9juFRvLpg-3D-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></span></div><br class=""></div></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=t5fkNsX-2F7-2FAygTFNPNE08PALgkjkO6I8wpsdssDS0ug33lTaVZn0fd9cwtBFtCx2P8WXWeQ5oi-2FN3l1WiJD60Be4aHzHtgCj67j8iUjV1ZZ24ZsW5NsTeMw-2BPlSPhbXGzRFpcJjlJGap8rBsyZTrs0jUhVyTyq0QKiGVMvbiW77gxVAGlVnuB7sl15UJPgYoC5BYGROQdFHQ-2BZXe5x5osn3-2FQXaRWpmQkot22kGCeUE-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div></div></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54OdnK3ob-2FgmBMLlaHExTN5ICWYES4GUkuqOfyJ5skCC9mFpwEErP013bVlGKqZiqL4WyP250LsqiVbVCohS84ES0jDneAEZEMYJsZSf-2FdsL92xRHLiI9l-2F8O1WnTea2baGr6Sm2RFCLPpYftqOXLiUEc25CZJXVxcUnvl-2BHRO-2BxNaxg6D9Rlh4LUunA7VveX0k-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></body></html>