Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] some: opaque result type #271

Open
vinhnx opened this issue Jun 5, 2019 · 1 comment
Open

[Swift] some: opaque result type #271

vinhnx opened this issue Jun 5, 2019 · 1 comment
Labels

Comments

@vinhnx
Copy link
Owner

vinhnx commented Jun 5, 2019

‪to me i just think of this way:

  • returning some FooProtocol is like we will tell the compiler that we will return “some sort of” generic type that has FooProtocol trait (some sort => opaque)
  • when we do this, compiler will infer the first returning type

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:

  • Our functions decide what type of data gets returned, not the caller of those functions.
  • We don’t need to worry about Self or associated type requirements, because the compiler knows exactly what type is inside.
  • We get to change our minds in the future whenever we need to.
  • We don’t expose private internal types to the outside world.

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 vinhnx added the Swift label Jun 5, 2019
@vinhnx vinhnx changed the title some: opaque result type [Swift] some: opaque result type Jun 5, 2019
@vinhnx
Copy link
Owner Author

vinhnx commented Jun 6, 2019

#270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant