#61: Opaque Return Types with some

Opaque return types let a function return a concrete type while exposing it as “some type that conforms to this protocol”.

func makeGreeting() -> some CustomStringConvertible {
    "Hello"
}

The caller knows the result conforms to CustomStringConvertible, but not which exact concrete type is returned.

This is useful when you want abstraction while still keeping a concrete type underneath the API.


I'm also on Twitter and GitHub.

Subscribe via RSS.