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

Dave Abrahams dabrahams at apple.com
Wed Apr 6 11:16:38 CDT 2016


on Wed Apr 06 2016, Jeremy Pereira <swift-evolution at swift.org> wrote:

> I think I would be -1 on this.
>
> The proposal is that there be two names, Self and self that refer to
> different objects but are differentiated only by the capitalisation of
> the first letter and these could be mixed in the same context.

No, Self would not refer to an object; Self is a type alias.  Self.self
is the metatype object that you might be thinking of.

> I think this would make code that uses both harder to read. If typing
> ‘self.dynamicType’ is too onerous, how about shortening it in a
> different way? Why, for instance is ‘self.' mandatory? Alternatively,
> why is it ‘dynamicType’ and not just ‘type’?

That's a completely different beast; not useful in the same contexts.
For example, you can't write

       var x: self.dynamicType

but you could write

       var x: Self

>
>> On 6 Apr 2016, at 10:51, Adrian Kashivskyy via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> +1 for Self and the proposal.
>> 
>> 
>> Pozdrawiam – Regards,
>> Adrian Kashivskyy
>> 
>>> Wiadomość napisana przez Bernd Ohr (jazzbox) via swift-evolution <swift-evolution at swift.org> w dniu 06.04.2016, o godz. 09:11:
>>> 
>>> I am using a typealias for this:
>>> 
>>> struct MyStruct {
>>>    private typealias _Self = MyStruct
>>> 
>>>    static func staticMethod() { print("staticMethod") }
>>> 
>>>    func instanceMethod() {
>>>        _Self.staticMethod()
>>>    }
>>> }
>>> 
>>> 
>>> 
>>>>> On Apr 5, 2016, at 4:17 PM, Timothy Wood<tjw at me.com(mailto:tjw at me.com)>wrote:
>>>>> 
>>>>>> On Apr 5, 2016, at 3:04 PM, Joe Groff via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
>>>>>> What you're describing should be spelled `Self`, IMO. I think
>>>>>> Tim intended `#Self` to mean the *static* type the code is
>>>>>> declared inside (which is the same as Self unless you're in a
>>>>>> class).
>>>>> 
>>>>> Yes, that is what I was aiming for. `#Self` would be a pretty
>>>>> much textual replacement just like #file, etc. That is, I could
>>>>> imaging it being used in a bunch of cases (not useful here, but
>>>>> just intending to enumerate the possible uses I see):
>>>> Please check to ensure that the changes I just made match your expectations:
>>>> 
>>>> https://gist.github.com/erica/c60c7d51809889f3dfd47cdb482d6227
>>>> 
>>>> -- E
>>>> 
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-- 
Dave



More information about the swift-evolution mailing list