Swift: Allow Self as an alias for the scope’s type

Originator:rix.rob
Number:rdar://17892696 Date Originated:01-Aug-2014 09:28 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6-Beta4 (6A267n)
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
Protocol methods can use Self to refer to the type which is conforming to the protocol. But you can’t copy/paste method declarations using Self into your type and just implement them, because Self is disallowed outside of protocols.

It would be very convenient to have Self be an alias for the specific type being implemented. For example, if I’m implementing a struct named Natural, then Self would refer to Natural:

struct Natural {
    let value: Int
    func succ() -> Self { … }
}

But it’s more interesting for parameterized types, where Self would implicitly include those. For example, an enum Result<T> would alias Self to Result<T>:

enum Result<T> {
    case Success(T)
    case Failure

    func and(other: Self) -> Self { … }
}

This would be exactly equivalent to explicitly providing a typealias named Self:

enum Result<T> {
    case Success(T)
    case Failure

    typealias Self = Result<T>
    func and(other: Self) -> Self { … }
}


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!