Sorry -1 from me. Not worth adding. Too few use cases. <span></span><br><br>On Sunday, 28 February 2016, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><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>
<br>
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>
<br>
However, I think that specific use case is better served by a method on Dictionary. Perhaps something equivalent to:<br>
<br>
mutating func initializeValue(value: Value, forKey key: Key) {<br>
let oldValue = updateValue(value, forKey: key)<br>
precondition(oldValue == nil, "Initialized \(key) when it was already set")<br>
}<br>
<br>
Why do I think this approach is better?<br>
<br>
* `:=` 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>
<br>
* 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>
<br>
* By putting the precondition inside `initializeValue(_:forKey:)` itself, the error message can actually include the key, which may aid in debugging.<br>
<br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">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>
</blockquote><br><br>-- <br>-- Howard.<br>