How to simplify struct mutating in Swift

Issue #354

In Construction, we have a build method to apply closure to inout struct.

We can explicitly define that with withValue

1
2
3
4
5
func withValue<T>(_ value: T, closure: (inout T) -> Void) -> T {
var mutableValue = value
closure(&mutableValue)
return mutableValue
}

So we can modify Protobuf structs easily

1
2
3
4
5
6
user.book = withValue(Book()) {
$0.price = 300
$0.author = withValue(Author()) {
$0.name = "Thor"
}
}

Comments