Proposal: Stored properties in extensions


(Tomáš Linhart) #1

Hello,

very often, it is necessary to define stored properties in extensions. It
is currently only possible in Swift that has access to ObjC runtime with
associated objects (in theory it might be possible with some global
dictionary) and it requires a lot of boilerplate. Associated objects are
used in UIKit quite heavily so it is something that authors of frameworks
need.

It would be nice to able to create properties in extensions natively in
Swift without need of Objective-C and without a boilerplate. These
properties could be marked with some extra keyword/attribute so it is
obvious they are different and they have a cost (depends on their
implementation).

What do you think?
Tomáš


(Jacob Bandes-Storch) #2

Sometimes it's desirable to break up a type's implementation into multiple
files, within the same module. So for this simple case, it make sense for
stored properties to be allowed (within the same module), treating them as
continuations of the original class/struct declaration with no additional
overhead.

···

On Sat, Dec 5, 2015 at 4:34 PM Tomáš Linhart <swift-evolution@swift.org> wrote:

Hello,

very often, it is necessary to define stored properties in extensions. It
is currently only possible in Swift that has access to ObjC runtime with
associated objects (in theory it might be possible with some global
dictionary) and it requires a lot of boilerplate. Associated objects are
used in UIKit quite heavily so it is something that authors of frameworks
need.

It would be nice to able to create properties in extensions natively in
Swift without need of Objective-C and without a boilerplate. These
properties could be marked with some extra keyword/attribute so it is
obvious they are different and they have a cost (depends on their
implementation).

What do you think?
Tomáš
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Paul Young) #3

There was some discussion about this recently on Twitter: https://twitter.com/jckarter/status/664257829633568768

···

On Sun, Dec 6, 2015 at 12:44 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org> wrote:

Sometimes it's desirable to break up a type's implementation into multiple
files, within the same module. So for this simple case, it make sense for
stored properties to be allowed (within the same module), treating them as
continuations of the original class/struct declaration with no additional
overhead.
On Sat, Dec 5, 2015 at 4:34 PM Tomáš Linhart <swift-evolution@swift.org> > wrote:

Hello,

very often, it is necessary to define stored properties in extensions. It
is currently only possible in Swift that has access to ObjC runtime with
associated objects (in theory it might be possible with some global
dictionary) and it requires a lot of boilerplate. Associated objects are
used in UIKit quite heavily so it is something that authors of frameworks
need.

It would be nice to able to create properties in extensions natively in
Swift without need of Objective-C and without a boilerplate. These
properties could be marked with some extra keyword/attribute so it is
obvious they are different and they have a cost (depends on their
implementation).

What do you think?
Tomáš
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution