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

Joe Groff jgroff at apple.com
Thu Apr 7 10:28:03 CDT 2016


> On Apr 6, 2016, at 11:09 PM, David Hart <david at hartbit.com> wrote:
> 
> There's something I find very confusing with this proposal, and it's how Self is already used in protocol definitions to represent the STATIC type of the type that conforms to the protocol. I think people will be potentially very confused by how Self represents different types in different contexts:
> 
> protocol Copyable {
>     func copy() -> Self
> }
> 
> class Animal : Copyable {
>     init() {}
>     func copy() -> Animal {
>         return Self.init()
>     }
> }
> 
> class Cat : Animal {}
> 
> In the previous sample, wouldn't it be confusing to people if Self in the protocol means Animal in the Animal type, but Self in the Animal type may mean Cat?

Protocol conformances are inherited, so that's not a valid conformance, and protocol Self is synonymous with class Self when a class conforms. Animal.copy() would have to return Self to conform to Copyable.

-Joe


More information about the swift-evolution mailing list