<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Dec 15, 2015 at 4:58 PM, Marc Knaup <span dir="ltr">&lt;<a href="mailto:marc@knaup.koeln" target="_blank">marc@knaup.koeln</a>&gt;</span> wrote:<br><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">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.</li></ul></div></div></blockquote><div>Consider using it multiple times with the same variable</div><div><br></div><div>    var value = someInitialValue</div><div>    value ??= fallback1</div><div>    value ??= fallback2</div><div><br></div><div>Of course, this could also be &quot;let value = someInitialValue ?? falback1 ?? fallback2&quot;, which isn&#39;t necessarily more readable, but arguably better because it uses let.</div><div><br></div><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"><div><ul><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></li></ul></div></div></blockquote><div>I think you&#39;re right that this is one of the most common cases. Without this proposal:</div><div><br></div><div>    if self.property == nil { self.property = newValue [possibly optional] }</div><div>    if let value = self.property {</div><div>        // ...</div><div>    }</div><div><br></div><div>or</div><div><br></div><div>    let value: T</div><div>    if let v = self.property { value = v }</div><div>    else { value = newValue [non-optional]; self.property = value }</div><div>    // ...</div><div><br></div><div>With this proposal:</div><div><br></div><div>    self.property ??= newValue</div><div>    if let value = self.property {</div><div>        // ...</div><div>    }</div><div><br></div><div>I think the second is cleaner, but the difference isn&#39;t huge. You could also have ??= return the new value, so you can embed it in a larger expression/optional-binding, but that&#39;s not consistent with any of the other compound assignment operators.</div><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"><ul><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.<br></li></ul></div></blockquote><div>I disagree that this would be unexpected. It&#39;s consistent with how ??, &amp;&amp;, and || work. (And now that ++ and -- are going away, it likely matters even less.)</div><div><br></div><div>Anecdotally, I introduced this operator about 4 months ago to a codebase with ~16,500 lines of Swift. It&#39;s been used 4 times.<br class=""><br clear="all"><div><div class="gmail_signature"><div dir="ltr">Jacob<br></div><div><br></div></div></div></div></div></div></div>