0
protocol A {}
extension Int: A {}
extension String: A {}

enum Result<T> {
    case success(T)
    case error(Error)
}

typealias BaseFunc = (Result<A>) -> ()

func foo(r: Result<Int>) {}
func bar(r: Result<String>) {}

var f: BaseFunc = foo

Compiler says cannot convert value of type '(Result<Int>) -> ()' to specified type 'BaseFunc' (aka '(Result<A>) -> ()')

Is what I'm trying to do possible in Swift? Maybe there is another way to achieve similar result?

@iWheelBuy answered correctly for this question. However I formulated it wrong. What I want is something like this:

protocol A {}
extension Int: A {}
extension String: A {}

enum Result<T> {
    case success(T)
    case error(Error)
}

typealias BaseFunc<T> = (Result<T>) -> ()

func foo(r: Result<Int>) {}
func bar(r: Result<String>) {}

var f: BaseFunc<A>
if arc4random_uniform(10) < 5 {
    f = foo
} else {
    f = bar
}

Which results in: cannot assign value of type '(Result<Int>) -> ()' to type '(Result<A>) -> ()'

1 Answers1

2

Have you tried this way?

typealias BaseFunc<B:A> = (Result<B>) -> ()

Your typealias doesn't have a generic parameter

iWheelBuy
  • 5,470
  • 2
  • 37
  • 71