& ldquo; The generic parameter 'T' could not be deduced & rdquo; error when overloading methods

advertisements

I have this small enum with 2 constructors. I am getting a "Generic parameter 'T' could not be inferred" error when I try to initialise using an Int argument.

import UIKit

public enum Result<T> {

case Success(T)
case Failure(Int)

   init( any: T) {
       self  = .Success(any)
   }

   init( number: Int) {
       self  = .Failure(number)
   }
}

let a = Result(any: "A String")
print(a)

let b = Result(number: 1)
print(b)

Is there any way to exclude Int from T or somehow give priority to the second initialiser?

Tested on XCode 7.3.1.


The issue is not that it's mistaking which constructor to use, the issue is that it needs to know right then when you're typing Result(number: 1) what type T will be. It cannot leave it as a wildcard T for runtime, it must be resolved at compile time.

So you may do this:

let b = Result<String>(number: 1) //letting it know that T would be a String.

The reason this is important is because -- say you wanted to put a bunch of Result in an array... Well... you can't put Result-Int in the same array as a Result-String (unless that array is of type Any), because it's very similar to putting an Int and a String inside an Array. In short, Result is not just one type, it's a set of a bunch of types.

I hope this is helpful to you!