#63: Self in Protocols and Classes

Self refers to the actual conforming or dynamic type, not just the type where the code was originally written.

protocol Copyable {
    func copy() -> Self
}

That makes Self very useful in protocols and fluent APIs where returning “the same concrete type” matters.

You will also see it in class methods and factory-style APIs. It is a small keyword, but it expresses a very precise constraint.


I'm also on Twitter and GitHub.

Subscribe via RSS.