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

Jeremy Pereira jeremy.j.pereira at googlemail.com
Thu Apr 7 06:30:18 CDT 2016


> On 6 Apr 2016, at 18:16, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> 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.

As I understand it, the proposal is that `Self` can be used as an alias to `self.dynamicType`, which is an object in the sense of entity in Swift but not an instance of a class or struct. 

My point, though is not the exact semantics of what you call the thing that `Self` is but that having it and `self` in the same code will lead to readability issues because you can have `self` and `Self` in the same code referring to completely different things and they only differ in the case of the first letter.

> 
>> 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 the _proposal_ is that `Self` can be used as an alias for `self.dynamicType`. I have no problem with using `Self` in places where a type is expected (well, no serious problem given it is already a done deal), but the proposal says “This proposal introduces Self, an equivalent to self.dynamicType.”

So you could have (in function bodies) 

    self.foo()	// invoke an instance method
    Self.bar()  // invoke a class method, same as self.dynamicType.bar()
    Self.foo()  // invokes a class method with the same name as the foo instance method

They mean different things and yet are distinguished only by the capitalisation of the letter S. I’m not objecting to a shorthand for self.dynamicType although I don’t agree with the proposal that self.dynamicType “fights against Swift’s goals of concision and clarity”, I only object to one that looks very similar to `self`. 


> 
> -- 
> Dave
> 
> _______________________________________________
> 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