[swift-evolution] Draft proposal: multi-property assignment .= operator

Douglas Gregor dgregor at apple.com
Sun Jan 10 21:29:27 CST 2016


> On Jan 10, 2016, at 12:21 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org> wrote:
> 
> (This proposal came from thinking about the memberwise initializer proposal as well as older proposals for "cascading" and creating scopes making some members act like local variables.)
> 
> I'd like to propose a syntax to set multiple properties at once. It would look like this:
> 
> 	var object = MyObject()
> 	object .= (
> 		property1: 1,
> 		property2: "a"
> 	)
> 
> and be equivalent to this:
> 
> 	var object = MyObject()
> 	object.property1 = 1
> 	object.property2 = "a"
> 	
> What the `.=` operator does is take each value in the tuple on the right and assign them to the property of the same name on the variable on the left. Assignments are performed in the same order as they're defined in the tuple.

For me, the scale of the problem isn’t large enough to warrant a language feature, especially given that...

> ## Tentative Implementation
> 
> It's almost possible already to implement this with reflection. Here's an attempt (using a different operator name because `.=` doesn't work as a custom operator):
> 
> 	infix operator ~= {  }
> 
> 	func ~= <T>(inout target: T, values: (a: Int, b: String)) {
> 		let valuesMirror = Mirror(reflecting: values)
> 		let targetMirror = Mirror(reflecting: target)
> 		valueLoop: for valueField in valuesMirror.children {
> 			guard let label = valueField.label else {
> 				fatalError("Missing label in value tuple.")
> 			}
> 			for targetField in targetMirror.children {
> 				print(targetField)
> 				if targetField.label == label {
> 					targetField.value = valueField.value
> 					continue valueLoop
> 				}
> 			}
> 		}
> 	}
> 
> This fails because you can't assign to fields using the Mirror API. If this line could be replaced by something that works:
> 
> 	targetField.value = valueField.value
> 
> then we could have a library implementation.


Assigning to fields using the Mirror API is a completely reasonable feature to add.

	- Doug



More information about the swift-evolution mailing list