<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="">I would have to give this a -1 as well.<div class=""><br class=""></div><div class="">On the bright side, I think this does what you are attempting to do, albeit with a different operator to work within the bounds swift gives for custom operators. I chose to use exclamation marks to highlight the failure condition will crash the application.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">infix</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">operator</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> !!= {</span><span style="font-variant-ligatures: no-common-ligatures" class="">associativity</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> right </span><span style="font-variant-ligatures: no-common-ligatures" class="">precedence</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff" class="">90</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> !!=<T>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">inout</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs:</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?, rhs:</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">precondition</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(lhs == </span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs = rhs</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(195, 34, 117);" class=""><span style="color: rgb(29, 148, 33);" class="">// Testing</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">nil</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c32275" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> b:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff" class="">1</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">a</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #294c50" class="">!!=</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff" class="">2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// 2</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">b</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #294c50" class="">!!=</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0435ff" class="">2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// EXC_BAD_INSTRUCTION</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">-DW</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><div><blockquote type="cite" class=""><div class="">On Feb 27, 2016, at 7:58 PM, Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Sorry -1 from me. Not worth adding. Too few use cases. <span class=""></span><br class=""><br class="">On Sunday, 28 February 2016, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> Here’s a use case. Suppose you are filling out a dictionary in some non-trivial way. However, it should be the case that the value for each key is computed and assigned only once. And so you would use the := operator to ensure this: dictionary[k] := value<br class="">
<br class="">
This is actually a great use case, and it's exactly the kind of thing you should mention when you suggest a change to Swift.<br class="">
<br class="">
However, I think that specific use case is better served by a method on Dictionary. Perhaps something equivalent to:<br class="">
<br class="">
mutating func initializeValue(value: Value, forKey key: Key) {<br class="">
let oldValue = updateValue(value, forKey: key)<br class="">
precondition(oldValue == nil, "Initialized \(key) when it was already set")<br class="">
}<br class="">
<br class="">
Why do I think this approach is better?<br class="">
<br class="">
* `:=` doesn't really explain what it does—if you've never seen the operator before, the most you can be sure of is that it's probably some kind of assignment. `initializeValue(_:forKey:)` *does* explain the intended semantic—this should set a value for the first time—which suggests that it shouldn't be used with a value that's already set.<br class="">
<br class="">
* I would need to see compelling examples of `:=` used outside of Dictionaries before I thought it would be useful as something applied to any lvalue. Dictionary is an unusual case because they have a dynamically-determined set of keys which are created simply by assigning them, so it's reasonable to not know whether a particular key is `nil` or not. Swift variables and properties, and most Swift collections, separate adding a value from updating it, so they may not need an analogous operation.<br class="">
<br class="">
* By putting the precondition inside `initializeValue(_:forKey:)` itself, the error message can actually include the key, which may aid in debugging.<br class="">
<br class="">
--<br class="">
Brent Royal-Gordon<br class="">
Architechies<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')" 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="">
</blockquote><br class=""><br class="">-- <br class="">-- Howard.<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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>