I have a type alias with parameter and I would like to return the instance of different parameter types from a method:
type TC[T] = (ClassTag[T], Option[T])
def gen(x: Int): TC[_] = x match {
case 0 => (classTag[Int], Option[Int](0))
case _ => (classTag[String], Option[String](""))
}
This does not work and gives me error:
error: type mismatch; found : (scala.reflect.ClassTag[_ >: Int with String], Option[Any]) required: TC[] (which expands to) (scala.reflect.ClassTag[$1], Option[_$1]) forSome { type _$1 }
And I tried to use Any instead of wildcard _, and it still does not work:
def gen(x: Int): TC[Any]
On line 2: error: type mismatch; found : scala.reflect.ClassTag[Int] required: scala.reflect.ClassTag[Any] Note: Int <: Any, but trait ClassTag is invariant in type T. You may wish to investigate a wildcard type such as
_ <: Any. (SLS 3.2.10) case _ => (classTag[String], Some("")) ^ On line 3: error: type mismatch; found : scala.reflect.ClassTag[String] required: scala.reflect.ClassTag[Any] Note: String <: Any, but trait ClassTag is invariant in type T. You may wish to investigate a wildcard type such as_ <: Any. (SLS 3.2.10)
How can this be achieved?