<div dir="ltr"><div><span style="line-height:1.5">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></div><div><span style="line-height:1.5"><br></span></div><div><div><span style="line-height:19.5px"> class MessagesViewController: UIViewController {</span></div><div><span style="line-height:19.5px"> var chatTitleName = "Default"</span></div><div><span style="line-height:19.5px"> }</span></div><div style="line-height:1.5"><br></div></div><div>As for the case Jacob mentioned above:</div><div><br></div><div><div> var value = someInitialValue</div><div> value ??= fallback1</div><div> value ??= fallback2</div></div><div><br></div><div>It seems like that can be written as:</div><div><br></div><div><div> var value = someInitialValue</div><div> ?? fallback1</div><div> ?? fallback2</div></div><div><br></div><div>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><br></div><div> var value = someInitialValue</div><div><br></div><div> var something</div><div> ...do some processing</div><div><br></div><div> value ??= something</div><div> guard value else { return }</div><div><br></div><div> var somethingElse</div><div> ...do some processing</div><div><br></div><div> value ??= somethingElse</div><div> guard value else { return }</div><div><br></div><div>But that can also be rewritten as something like:</div><div><br></div><div><div> var value = someInitialValue</div><div> ?? {</div><div> var something</div><div> ...do some processing</div><div> return something</div><div> }()</div><div> ?? {</div><div> var somethingElse</div><div> ...do some processing</div><div> return somethingElse</div><div> }()</div></div><div><br></div>Again, curious what people think about the style.<div><div><div><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 16, 2015 at 9:28 AM Marc Knaup via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm still uncertain whether that would solve the issue at the right location.<div><br></div><div>How do you end up in such a situation where this is actually necessary?</div><div>Why do I not end up in such situations?</div><div>I'd like to understand where the discrepancy comes from.</div><div><br></div><div>I.e. where do you define chatTitleName? Where else do you set chatTitleName?</div><div>Why isn't it initially set to "Default" an then overwritten on-demand?</div>
</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:22 PM, Kevin Wooten <span dir="ltr"><<a href="mailto:kdubb@me.com" target="_blank">kdubb@me.com</a>></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"><br><div><div><div><blockquote type="cite"><div>On Dec 16, 2015, at 4:12 AM, Al Skipp via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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 "var x = y ?? z" 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 "object.property = object.property ?? …" or using "if object.property == nil { … }".<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></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><span><div><br></div><blockquote type="cite"><div><div style="word-wrap:break-word">
<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">
</div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></span></div><br></div></blockquote></div><br></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">
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div></div></div></div></div>