vinhnx / notes

my today I learn (TIL) journal, includes everything that I found interesting, not necessarily relates to programming
44 stars 2 forks source link

[Swift] some: opaque result type #271

Open vinhnx opened 5 years ago

vinhnx commented 5 years ago

‪to me i just think of this way:

another way to describe some in my understanding is:

"inferred generic return type"

my finding: https://mobile.twitter.com/vinhnx/status/1136585951524687872

==

some... is an opaque result type as introduced by SE-0244 and is available in Swift 5.1 with Xcode 11. You can think of it as a "reverse" generic placeholder which is satisfied by the implementation rather than the caller.

func doSomething() -> some SomeProtocol {}

// is equivalent to:
func doSomething<T: SomeProtocol>() -> T {}

reference: https://stackoverflow.com/questions/56433665/what-is-the-some-keyword-in-swiftui

==

So, opaque result types allow us to do several things:

https://www.whatsnewinswift.com/?from=5.0&to=5.1

==

note :

think of some Protocol as "some sort" of Protocol SE-0244: https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md

==

https://blog.vihan.org/opaque-types-in-swift/

vinhnx commented 5 years ago

270