I have a sample class with custom implementation for a specific type:
class Test<T> {
fileprivate var v : T
init (_ val : T) { v = val }
func t() -> T { print("Default. \(T.self)"); return v }
}
extension Test where T == UInt8 {
func t() -> T { print("Specific. \(T.self)"); return v }
}
Sample use:
_ = Test<UInt8>(10).t() // Specific. UInt8
_ = Test<Int>(5).t() // Default. Int
Now I'm trying to traverse through the function
func call<T>(t type: T.Type) -> T {
return Test<T>(8).t() // 'Int' is not convertible to 'T'
}
_ = call(t: UInt8.self)
_ = call(t: Int.self)
How resolve this error or how use the protocol if method return generic value?