Created
November 3, 2020 18:05
-
-
Save dam5s/2398759ce8deb46a3afdd1d34fc39ee8 to your computer and use it in GitHub Desktop.
Limitations (?) to Swift extensions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Async<Wrapped> { | |
func map(function: @escaping (Wrapped) -> NewWrapped) -> Async<NewWrapped> { | |
// ... | |
} | |
// ... | |
} | |
typealias AsyncResult<Success, Failure> = Async<Result<Success, Failure>> | |
extension AsyncResult { | |
// The function below cannot be declared because there are no Success or Failure types in scope | |
func mapSuccess<NewSuccess>(function: @escaping (Success) -> NewSuccess) -> AsyncResult<NewSuccess, Failure> { | |
// ... | |
} | |
// ... | |
} |
You can constrain the function's generic New
or the instance's generics Success, Failure
or both.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you can get what you're after without the typealias by adding a where clause to your extension function: