<div dir="ltr">I&#39;ve started a formal proposal here:<div><br></div><div><a href="https://github.com/apple/swift-evolution/pull/63">https://github.com/apple/swift-evolution/pull/63</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 4:48 PM, Dave Abrahams via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Dec 16, 2015, at 6:22 AM, Kevin Wooten via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:AvenirNext-Regular;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><br>On Dec 16, 2015, at 4:12 AM, Al Skipp via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On 16 Dec 2015, at 00:58, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><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">I tend towards -1 for multiple reasons:<div><ul><li>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 &quot;var x = y ?? z&quot; is satisfying as it creates a non-optional value iff z is non-optional.<br><br></li><li>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><br></li><li>The remaining cases could expressed like &quot;object.property = object.property ?? …&quot; or using &quot;if object.property == nil { … }&quot;.<br>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><br></li><li>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>object.property ??= val</div><div><br></div><div>Instead of:</div><div>object.property = object.property ?? val</div><div><br></div><div>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><br></div><div>After perusing our Swift code it turns out that we use the long form (a = a ?? def) quite a bit.  As it was previously mentioned it, when the variables is named “a” it’s clearly not an issue, but this is…</div><div><br></div><div>    messagesViewController.chatTitleName =  messagesViewController.chatTitleName ?? “Default”</div><div><br></div><div>(Those are effectively real world variable names). </div><div><br></div><div>I think quite a bit of the clarity of this statement is lost by the duplication and the proposed form..</div><div><br></div><div>    messagesViewController.chatTitleName ??= “Default”</div><div><br></div><div>clears it up fairly well.</div></div></div></blockquote><br></div></span><div>A few points:</div><div><br></div><div>1. I&#39;ve always thought we needed something like this; glad to see it discussed</div><div><br></div><div>2. This is also applicable to dictionaries:</div><div><br></div><div>  <span style="font-family:AvenirNext-Regular">messagesViewController.titleNames[&quot;chat&quot;] ??= &quot;Default&quot;</span></div><div><span style="font-family:AvenirNext-Regular"><br></span></div><div><div><span style="font-family:AvenirNext-Regular">3. I think it may be time for a formal proposal :-)</span></div><div><span style="font-family:AvenirNext-Regular"><br></span></div><div><span style="font-family:AvenirNext-Regular">4. One way the community can help us to evaluate it would be to create the API in an extension in your own code, actually apply it in your project, and evaluate what it does for readability.</span></div></div><div><br></div><div>Thanks,</div><div><br></div><div>-Dave</div><div><div><div><br></div><br>

</div>
<br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=xV0JY-2FdZMnUMvSFtZnLiBPRTDDOSQf3-2FpH33HYOlBxE8Dztf2igHB4G3uzzyiEeIq83AyeZQumQgYLrHamdS72mfCKe6-2Bhr77A4r-2BU7KOwmQw9JzwH5BmhAb5ifzhMr2ULdKDnnqtfwrq6G3nEAd0TrjjAJIIWOoXweaU1Rz2ds7HQCAL6-2FsPufaRiTp0wpEyds1QGJI96C1Q8p0cXa0OFmpjsQwHGMOId4XILZwDGc-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">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div>+44 7523 279 698</div></div></div></div></div></div>
</div>