[swift-evolution] [Pitch] Self's nominal restriction denies significant feature patterns

Joe Groff jgroff at apple.com
Sat Jun 3 10:51:56 CDT 2017


> On Jun 2, 2017, at 3:08 AM, Zaid Daghestani via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Wow, you’re right. Look like this is a bug? The issue shows up in class definitions, not protocol definitions.	
> 
> This works:
> 
> protocol Selfie {
>     static func retOne() -> Self
>     static func retMany() -> Array<Self>
>     static func retTuple() -> (Self, Int)
> }
> 
> This doesn’t work:
> 
> class Selfie {
>     required init() {}
>     // Compiles
>     static func retOne() -> Self {return self.init()}
>     // Doesn't Compile
>     static func retMany() -> Array<Self> { return [] }
>     // Doesn't Compile
>     static func retTuple() -> (Self, Int) { return (self.init(), 0) }
>     
> }
> 
> So Self in non-nominal types with class methods end up with the compiler error:
> 
> Error: 'Self' is only available in a protocol or as the result of a method in a class; did you mean ’Selfie’?
> 
> Can we get confirmation from anyone?

Yes, `Self` in classes is currently restricted to being the return type of a method. This could eventually be generalized to allow `Self` in any covariant position within a class method type, and in fact SE-0068 (https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md) was accepted to allow this generalization, but we haven't done the implementation work to enable it yet.

-Joe


More information about the swift-evolution mailing list