Allow calling type members from instance members without `Self.`

Following up on the previous discussion, I wanted to refine my suggestion with the one Svein proposed.

Make the prefix Self. non-required when calling a type member (static or class) from an instance member. This can apply to botb methods and properties.

A great example was provided by Jessy. It also shows that the language sort of does this already in other cases.


enum ReallyOutThere {
  static func outerStaticMethod() { }
  static func reallyOutThereMethod() { }

  enum Outer {
    static func outerStaticMethod() { }

    struct Inner {
      static func innerStaticMethod() { }

      func instanceMethod() {
        innerStaticMethod() // will be possible after proposed change
        outerStaticMethod() // already compiles
        reallyOutThereMethod() // already compiles
      }
    }
  }
}

This way, the change is completely additive and won't break any existing code.

I believe it should be made to allow better idiomatic code and for better syntax symmetry.

22 Likes

I think it would make a lot of sense to extend that to constants (I mean "constant constants" ;-):
It is tempting to declare those (font sizes, margins…) as normal properties, just because that is more convenient.

Although afaics not nearly as common, I'd include static variables as well (I'm not sure if that is a complete list - but for type subscripts, I think it wouldn't work :smiley: )

3 Likes

I've edited the OP to reflect Tino's comments. Thanks for the suggestion!

1 Like

Yes please. That would allow me to move back a number of things from globals and properties to statics (where I originally wanted them).

Can someone point me to where I should attempt implementing it?

This would be fabulous, +1

Terms of Service

Privacy Policy

Cookie Policy