Mutating Init

I was writing some asynchronous code and ran into the following error on multiple accounts:

Error: Escaping closure captures mutating 'self' parameter

Whenever I need to capture a mutating instance of self, I must call a mutating function on the type itself after it has been initialized. You capture mutating self in a mutating get, set, willSet, didSet, and mutating func. This makes sense because the to call these in the first place the callee must be mutable. I thought that if you should be able to write functions for mutating instances in all of the aforementioned ways, why should you not be able to write initializers for mutating instances?

To solve this discrepancy I was thinking something like mutating init might be in order wherein you can capture mutating self in an escaping closure. A mutating init would have to ensure that the instance it is creating is mutable (var not let).

Another benefit of this would be that it would allow a type to enforce mutability on its instances which could be particularly useful for types that are liked to an online database for example.

Is there any precedence for something like this in Swift?

I’m not sure if this is possible:


Thank you @suyashsrijan for the explanation. I guess I'll just have to find other ways around it.