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

Erica Sadun erica at ericasadun.com
Mon Apr 4 13:17:50 CDT 2016


> On Apr 4, 2016, at 12:13 PM, Joe Groff <jgroff at apple.com> wrote:
> 
>> 
>> On Apr 4, 2016, at 11:00 AM, 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`.
> 
> There's the wrinkle of inheritance, as there so often is. `Self` inside a class scope already means "the dynamic class of 'self'", not "the type this declaration statically appears within". Now, we really ought to allow you to utter Self in the bodies of class methods too. It would be consistent to extend that courtesy to value types, where dynamic `Self` always matches the static type, from that principle.
> 
> -Joe

Would using another word or symbol fix that problem?

-- E

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160404/4f7a47dd/attachment.html>


More information about the swift-evolution mailing list