Organizing stored properties in extensions

A very common pattern for organizing methods is using extensions within the module a type is declared in, in order to group methods semantically or for some other reason. These methods behave as if they are declared within the initial declaration itself and UIKit also uses this system of organization in its (generated) Swift interfaces. However, we can't do this today with properties because all stored properties need to be declared within the initial declaration.

Thoughts on relaxing this constraint within the module a type is declared in?

13 Likes

It's a good idea, and one I've often wanted. For both logical and implementation reasons, it makes a lot of sense to start with (1) same-module extensions (2) of concrete types (i.e. you can't add stored properties to protocols (probably ever) or UIKit classes (yet)).

There's been some discussion of this in the past:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161003/027723.html

5 Likes

I have also wanted this for a long time, for the same reasons.

Though I thought it would be limited to extensions in the same file rather than the whole module - similarly to how synthesized protocol conformances only work if declared in the same file as the primary declaration.

3 Likes