Lets say i have a protocol:
protocol Router {
associatedtype Answer
typealias AnswerCallback = (Answer) -> Void
}
At some point I want to store a variable of type AnswerCallback
var answerCallback: Router.AnswerCallback ...
But I need to specify which concrete type i'm using since i get this error:
Type alias 'AnswerCallback' can only be used with a concrete type or generic parameter base
How I can specify a type like Router.AnswerCallback ... where String is "Answer" type?
Instead the only way to work is to use var answerCallback: ((String) -> Void)
You need a class/struct that conforms to Router, which has an Answer of String:
class StringRouter : Router {
typealias Answer = String
}
let callback: StringRouter.AnswerCallback? = nil
If all you want is such a type alias, you don't need a protocol:
typealias AnswerCallback<T> = (T) -> Void
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With