<div dir="ltr">Agreed, IMO it would be quite dangerous for "a ??= b" to mean anything other than "a = a ?? b".<div><br></div><div>On another note, I don't see the value of "a? = b". I had never realized before that this works. Is this feature actually used in the wild? Should we consider removing it? (I could perhaps see some value if the assignment operator were overloadable, but it's not.)</div><div class="gmail_extra"><br clear="all"><div><div class="m_3617066003682949734gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Wed, Jan 25, 2017 at 10:28 AM, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.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"><div><span><blockquote type="cite"><div>On Jan 25, 2017, at 12:47 PM, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div>Really? My observation from a quick test is that "a? = b" assigns b to a if a already has a value, or does nothing if it's nil. This is sort of the opposite of what's being proposed, which is that "a ?= b" should assign to a only if it does NOT have a value.<br></div></blockquote><div><br></div></span>Right. On the other hand, this does seem like a poor spelling for the operator, given the ease of confusion.</div><div><br></div><div>Also, I'm finding it hard to imagine a use for this where the equivalent ?? invocation wouldn't be *much* clearer. It just feels like you must be doing something backwards — "I've filled in a default value for this variable, now overwrite it if this other value exists". Wouldn't the reverse generally be better?</div><span class="m_3617066003682949734HOEnZb"><font color="#888888"><div><br></div><div>John.</div></font></span><div><div class="m_3617066003682949734h5"><div><br><blockquote type="cite"><div><div class="gmail_quote"><div dir="ltr">On Wed, Jan 25, 2017 at 9:33 AM Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">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"><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> On Jan 25, 2017, at 8:40 AM, Nichi Shin via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="m_3617066003682949734m_8108690658268912376gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> I’d like to propose a new operator for optional assignment in Swift.<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> The idea is that by using this operator (e.g. by doing a ?= b), the optional on the right would be assigned to the variable on the left only when it has something to assign (i.e. when it's not nil).<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
`a? = b` already does this. Maybe we need a fixit to make that more apparent, though.<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
-Joe<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> The implementation could be something as follows:<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> /// Optional Assignment Operator<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> infix operator ?=: AssignmentPrecedence<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> func ?=<T>(left: inout T, right: T?) {<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> if right != nil {<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> left = right!<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> }<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> }<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> func ?=<T>(left: inout T?, right: T?) {<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> if right != nil {<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> left = right<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> }<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> }<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> I hope you will consider adding this on a future release of this great programming language.<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> Kind regards,<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> N. S.<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> ______________________________<wbr>_________________<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> swift-evolution mailing list<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> <a href="mailto:swift-evolution@swift.org" class="m_3617066003682949734m_8108690658268912376gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_3617066003682949734m_8108690658268912376gmail_msg" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
______________________________<wbr>_________________<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
swift-evolution mailing list<br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="m_3617066003682949734m_8108690658268912376gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_3617066003682949734m_8108690658268912376gmail_msg" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br class="m_3617066003682949734m_8108690658268912376gmail_msg">
</blockquote></div>
______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div>