[swift-evolution] Optional assignment operator

Jose Manuel Sánchez Peñarroja josema at vitaminew.com
Thu May 12 08:59:59 CDT 2016


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


More information about the swift-evolution mailing list