[swift-evolution] Optional assignment operator

Rod Brown rodney.brown6 at icloud.com
Thu May 12 09:41:54 CDT 2016


I’m tentatively supportive of this proposal. I definitely see the use case (assign only if not not nil).  Interested to hear the opinions of others here :)

-Rod


> On 12 May 2016, at 11:59 PM, Jose Manuel Sánchez Peñarroja via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Sorry if this has already been discussed, if so I couldn’t find it.
> 
> I would like to propose to add to Swift an optional assignment operator ?=
> I think this would nicely align with the other uses of ?, and avoid repetition in this case:
> 
> 	var value = 5
> 
> 	var possibleNewValue: Int? = nil
> 
> 	value = possibleNewValue ?? value
> 
> It would be used like this:
> 
> 	value ?= possibleNewValue
> 
> I’ve found quite a few cases in which this would be very useful to me. It is already possible to implement it, but having it defined in the standard library would define an standard, and prevent different semantics depending on who implements it.
> 
> 
> 	infix operator ?= {
>  	  associativity right
> 	   precedence 90
> 	   assignment
> 	}
> 
> 	func ?= <T>(inout lhs: T, rhs: T?) {
> 	    lhs = rhs ?? lhs
> 	}
> 
> 
> Regards,
> José Manuel Sanchez
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list