<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:03 AM, Stephen Celis <<a href="mailto:stephen.celis@gmail.com" class="">stephen.celis@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">While I used `||=` in Ruby all the time, where everything can be `nil`, I've found that Swift's Optional model is different enough that I've never really desired a `??=`, so I'm a -1 for the proposal, as I see it encouraging the use of optionals where a non-optional would be preferred.<div class=""><br class=""></div><div class="">With the `chatTitleName` example, I'd probably make `"Default"` the default argument on initialization (and make it so `chatTitleName` is not optional), or I'd move the `chatTitleName ?? "Default"` into the presentation logic.<br class=""></div><div class=""><br class=""></div></div></div></blockquote><div><br class=""></div>Well I was using real variable names but the example was contrived; I should have made that clearer. Also, I didn’t mean to sound like I was in clear support of this addition. I was really just putting it into context with regard to variables names where we’ve used “??”. </div><div><br class=""></div><div>In reality my vote is.. ±0<br class=""><div><br class=""></div><blockquote type="cite" 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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="">Stephen</div></div><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 9:28 AM, Marc Knaup via swift-evolution<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: 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><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:22 PM, Kevin Wooten<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:kdubb@me.com" target="_blank" class="">kdubb@me.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: 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 <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> 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. 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=""> <span class="Apple-converted-space"> </span>messagesViewController.chatTitleName = messagesViewController.chatTitleName ?? “Default”</div><div class=""><br class=""></div><div class="">(Those are effectively real world variable names). </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=""> <span class="Apple-converted-space"> </span>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 alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !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 alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""></div></div><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" 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></blockquote></div></div></div></blockquote></div><br class=""></body></html>