I'm trying to write a custom assignment operator in Swift 3 that will allow me to set a variable and update its properties all at once. Like so,
protocol BatchSettable { }
infix operator =<
public func =< <T: BatchSettable>(lhs: inout T?, rhs: (inout T) -> Void) {
if var copy = lhs {
rhs(©)
lhs = copy
} else {
lhs = nil
}
}
This would, in theory, allow me to write
struct MyStruct: BatchSettable {
var someBoolProperty: Bool?
var someStringProperty: String?
}
self.myStruct =< {
$0.someBoolProperty = false
$0.someStringProperty = "someString"
}
instead of
var myVar = self.myStruct
myVar.someBoolProperty = false
myVar.someStringProperty = "someString"
self.myStruct = myVar
My intention here is to be able to update multiple fields on a value-type property (struct) without triggering didSet
multiple times.
The operator itself compiles with no errors, but when I try to use it I get
Cannot assign to property: '$0' is immutable
Any ideas?