let f: (inout Int) -> Void = { y in y += 1 }
var x = 0
f(&x)
This is not quite what I want. The C++ lambda in question doesn't have any parameters, it has one capture only.
What I want is for this to be inout. Which is not the same as capturing "this" by reference, in that case the captured "this" is mutable but the lambda context itself is still immutable.
Are you looking for a feature demonstrated by the second example in this answer? I don't think Swift supports it (because I have never read about it). Just curious, why does Swift's capturing by reference not work in your case?
In SwiftUI you can use @State property wrapper for this purpose:
struct MyView: View {
@State var foo: Int = 4
}
Can you please provide a code snippet and the problem you try to solve. It seems there is a wrong focus in this thread. While it is interesting to do a 'closure that mutate value-type instance' on its own, it seems that this is a wrong direction of solving your concrete SwiftUI task.