# [swift-evolution] [RFC] #Self

Matthew Johnson matthew at anandabits.com
Tue May 10 12:50:21 CDT 2016

```

> On May 10, 2016, at 12:33 PM, Vladimir.S <svabox at gmail.com> wrote:
>
> As I understand, please correct me, this proposal will not solve this problem. The proposed solution just to introduce #Self as replacement for concrete name of currently declared type.
>
> And actually I think(I don't understand?) something is wrong with what you want to achieve..
>
> Let's imagine we have non-final class:
>
> class C {
>    static func f() -> C { return C() }
> }
>
> and, as class is not final, we can have subclasses :
>
> class D : C {
> }
>
> Now we introduce the protocol:
>
> protocol A {
>    static func f() -> #Self  // let's imagine #Self can be in this place
>                          // in test code I used C instead of #Self here
> }
>
> And conform C to A, so this says "C will return C when we call f()"
>
> extension C: A {
>  // let's imagine it is OK here, C.f() returns #Self which is C
> }
>
> But now, we have a problem with D - as C conforms to protocol A, D also conforms to protocol A :
>
> var c = C()
> var d = D()
>
> if c is A {print("c is A")}  // c is A
> if d is A {print("d is A")}  // d is A
>
> But...
>
> print(C.f())  // main.C
> print(D.f())  // main.C oops! D.f() does not return its #Self, i.e. main.D

No, the whole point is that D.f() returns C because C is the requirement of 'f' is declared to return #Self which is C where the protocol conformance is declared and implemented.  If you want a covariant requirement you would use Self as the return type, not #Self.

>
>
> On 10.05.2016 19:37, Matthew Johnson wrote:
>>>> Could you please illustrate this in a couple lines of code? Just to
>>>> fully understand.
>> protocol A { static func createWithString(s: String) -> Self }
>>
>> extension NSURL: A { // cannot conform because NSURL is non-final }
>>
>> If we could define a protocol requirement that didn't covary (using
>> #Self or whatever) we would be able to write the desired conformance.
>>

```