[swift-evolution] Proposal: An assignment operator := that ensures nil before assignment.

David Waite david at alkaline-solutions.com
Sat Feb 27 23:17:04 CST 2016


I would have to give this a -1 as well.

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.

infix operator !!= {associativity right precedence 90 }

func !!=<T>(inout lhs:T?, rhs:T) {
  precondition(lhs == nil)
  lhs = rhs
}

// Testing
var a:Int? = nil
var b:Int? = 1

a !!= 2 // 2
b !!= 2 // EXC_BAD_INSTRUCTION

-DW

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160227/fdd1e1f3/attachment.html>


More information about the swift-evolution mailing list