[swift-evolution] [Pitch] Adding a Self type name shortcut for static member access

Sean Heber sean at fifthace.com
Mon Apr 4 13:07:52 CDT 2016


I’ve wondered this as well. Using “self.dynamicType” is noisy and somewhat esoteric, and using “MyStruct.foo” is bad the moment you want to rename “MyStruct”.

l8r
Sean


> On Apr 4, 2016, at 1:00 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Are there reasons that prevent using `Self` as a synonym for an instance's type name?
> 
> Consider:
> 
> struct MyStruct {
>     static func foo() { print("foo") }
>     func bar() {
>         MyStruct.foo() // works
>         self.dynamicType.foo() // works
>         Self.foo() // error
>     }
> }
> 
> Obviously, you can always name a type directly or use `self.dynamicType` but
> neither solution does any favors for readability. Both approaches obscure intent, 
> especially as type names grow large: `MyExtremelyLargeTypeName.staticMember`,
> for example. Plus, as Kevin B pointed out to me,  `self.dynamicType.classMember`  
> and `TypeName.classMember` may not be synonyms in class types with non-final members.
> 
> I'd like to see `Self.staticMember` introduced as a synonym for `TypeName.staticMember`.
> 
> Thoughts?
> 
> -- E
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list