[swift-evolution] [Pitch] Tweak `Self` and introduce `Current`

Adrian Zubarev adrian.zubarev at devandartist.com
Sat Jan 7 04:47:12 CST 2017


As a naming contrast, one could make Self like the proposed Current, and rename the current Self to DynamicSelf or something similar.

Most protocols that are meant for value types in first place should use Self aka. Current as the return type.



-- 
Adrian Zubarev
Sent with Airmail

Am 7. Januar 2017 um 11:38:55, Adrian Zubarev (adrian.zubarev at devandartist.com) schrieb:

Hi Swift community,

I’d like to talk to about current Self keyword. If I’m not totally mistaken then the current Self has a few meanings:

Refer to the current type, or refer to the dynamic type for non-final classes inside containing type (SE–0068 - not yet implemented).
For non-final class types use Self as return type on the conforming super type (or return an instance of receiver Self).
Let me visualize the behaviors quickly in some short code snippet:

protocol Foo {
    func foo(_ f: Self) -> Self
}

class A : Foo {
    // forced to use `A` as parameter type and `Self` as return type
    func foo(_ f: A) -> Self { return self }
    // Returning `A()` would cause an error: Cannot convert return expression of type 'A' to return type 'Self'
    func bar() -> A { return A() /* or self */ }
    func zoo() -> Self { return /* only */ self }
}

class B : A {
    // Both is fine `B` or `Self` as the return type
    // If `B` is used you can return a different instance like `B()`
    // `Self` does only allow `self` to be used here
    override func foo(_ f: A) -> B { return self }
}

struct D : Foo {
    // No `Self` allowed here at all
    func foo(_ f: D) -> D { return self /* or D() */ }
}
The behavior of Self is a little magical, because it sometimes refers to the current type it is used in, or it has a contract of using self.

I propose of introducing a new keyword called Current to solve a few problems here.

Self on parameter types would be disallowed for protocol members, because conformances to that protocol already disallow that (see A above). Instead one would use Current and get the correct meaning.

protocol Boo {
    func boo(_ b: Current) -> Self
}
      
procotol Loo {
    func loo() -> Current
}
      
class X : Boo, Loo {
    func boo(_ b: X) -> Self { return self }
    func loo() -> X { return self /* or X() */ }
}
      
final class Y : Boo {
    func boo(_ b: X) -> Y { return self /* or Y */ }
}
Using Self inside the containing type would always mean as one would refer to the dynamic type, like the magical syntax function type(of:) does.

Current can only refer to the current containing type.

On classes Self has always the contract of returning self.

Self could be discouraged in favor of Current on value types, as a shorthand to refer to the containing type.

Generics could benefit from Current too:

extension AReallyLongNonFinalClassName {
    func casted<T : Current>() -> T { ... }
}
// `Self` wouldn't here, because it would refer to the dynamic type
#1 Would affect a lot of protocols which implies that it would affect ABI.

These are the first ideas I had in my mind. We can polish it further if it receives positive and constructive feedback.

Best regards,



-- 
Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170107/327c4648/attachment.html>


More information about the swift-evolution mailing list