Proposal - Allow properties in Extensions


(Kevin Kachikian) #1

I’d like to proposal to the Swift community and discuss the advantages and disadvantages of adding modifiable properties to extensions (in addition to the already existing computed properties, instance methods, subscripts, etc.):

extension SomeType {
  
  var aNewProperty: Int
  var anotherVariable: String
  var aThirdOne: MyStruct

}

I have experienced many occasions where adding a a property to an extension would have resulted in cleaner code by keeping all similar code locally together in a file. Not knowing much about language design, I don't know if this offering this might break some fundamental principal in OOP design. Curiously ObjC also had this limitation but apparently C# offers the capability in partial classes.

One such use case is when extending ones own code, it's nice to break similar code blocks/behavior into their own extensions sometimes requiring the addition of properties. It would be nice to provide those properties right in line with the new code instead of just having a bunch of properties forced at the top of a main class file.

Wha-da-ya’all think?

Kevin


(Chris Lattner) #2

IMO, this is a generally desirable thing. It would be great to replace the need to use objc_setAssociatedObject with proper language support. That said, the exact semantics and implementation details are unclear, so we’d need a specific detailed proposal.

-Chris

···

On Dec 8, 2015, at 10:51 AM, Kevin Kachikian via swift-evolution <swift-evolution@swift.org> wrote:

I’d like to proposal to the Swift community and discuss the advantages and disadvantages of adding modifiable properties to extensions (in addition to the already existing computed properties, instance methods, subscripts, etc.):

extension SomeType {
  
  var aNewProperty: Int
  var anotherVariable: String
  var aThirdOne: MyStruct


(Kevin Kachikian) #3

I never quite understood why ObjC never implemented such a capability as it seems really useful.

I will have to defer the details however to someone else in the community as defining the semantics and implementation details are currently above my pay grade. {:~)

-Kevin

···

On Dec 8, 2015, at 9:21 PM, Chris Lattner <clattner@apple.com> wrote:

On Dec 8, 2015, at 10:51 AM, Kevin Kachikian via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I’d like to proposal to the Swift community and discuss the advantages and disadvantages of adding modifiable properties to extensions (in addition to the already existing computed properties, instance methods, subscripts, etc.):

extension SomeType {
  
  var aNewProperty: Int
  var anotherVariable: String
  var aThirdOne: MyStruct

IMO, this is a generally desirable thing. It would be great to replace the need to use objc_setAssociatedObject with proper language support. That said, the exact semantics and implementation details are unclear, so we’d need a specific detailed proposal.

-Chris