0

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?

qRoC
  • 183
  • 1
  • 4
  • 13
  • Compare [Wrong specialized generic function gets called in Swift 3 from an indirect call](http://stackoverflow.com/q/41980001/2976878) – from inside `call(t:)`, the only overload of `t()` that the compiler can dispatch to is the one in main class declaration, as it's unknown whether `T` is a `UInt8`. However, when calling `t()` directly, it is known, thus the extension method can also be dispatched to. – Hamish Apr 04 '17 at 18:10
  • I agree this is a duplicate, in that all the same issues are addressed in the same answer and we should consolidate them there. If you (@qRoC) believe there's a separate issue here that needs a separate answer, comment and we can reopen it, but I'm marking it dupe for now. – Rob Napier Apr 04 '17 at 18:59

0 Answers0