Skip to content

Instantly share code, notes, and snippets.

@dam5s
Created November 3, 2020 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dam5s/2398759ce8deb46a3afdd1d34fc39ee8 to your computer and use it in GitHub Desktop.
Save dam5s/2398759ce8deb46a3afdd1d34fc39ee8 to your computer and use it in GitHub Desktop.
Limitations (?) to Swift extensions.
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> {
// ...
}
// ...
}
@BrianSemiglia
Copy link

BrianSemiglia commented Nov 3, 2020

I think you can get what you're after without the typealias by adding a where clause to your extension function:

extension AsyncResult {
  func mapSuccess<New>(function: @escaping (Success) -> New) -> AsyncResult<New, Failure> where Success == Result<New, Failure> {
    // ...
  }
}

@BrianSemiglia
Copy link

BrianSemiglia commented Nov 3, 2020

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