Extension is a powerful feature in Swift, allowing type extensibility in various way. However, there are some limitations that everyone has to cope with. We cannot add stored variables to extension scope. ( enclosed variable )
To workaround with the problem, associated object has been used so far by many Swift/Obj-c library that we are using today. This technique facilitates the power of extension. It allows extension to have enclosed stored property at runtime. However, we can only apply this method to NSObject subclass type. And I think it is some kind of hack, and when it comes to Swift the syntax looks messy.
So, I would like to propose an idea of storing variable inside extension scope. 'enclose', is a prefix keyword, stating that a variable is only visible inside an extension block scope.
enclose keyword only allow to be used in extension scope
enclose var count = 1 // count is visible only in this extension scope.
count = count + 1
// another extension scope
// can't see 'count' because it's in another extension scope.
print("do whatever but you will not see 'count' in this")
This allows mixins composition style. It has no side-effect to other parts, and is considered to have better separation of concerns. Since we have protocol extension in Swift 2.0, this feature will empower composition scheme, and eliminates associated object hacking from Swift/Obj-c.
Think about rewriting massive view controller as extensions composition. We don't have to store all states of the view controller class in one place. We can separate it to multiple part, each part consists of it own states using enclose keyword.
I'm pretty sure it will be very useful. So, I would like to ask the opinions of you guys about pros and cons of doing this. What might be an alternative solution? Is it consistent ?