实际上t 和"some string" -> t 都会在运行时出现同样的问题:
import scala.language.existentials
import scala.reflect.runtime.universe._
type SubMode[T] = M forSome { type M <: Mode[T] }
val t: MyType[_, _] = new MyType[String, SubMode[String]] {}
// t: MyType[_, _] = $anon$1@596afb2f
"some string" -> t
// error: type arguments [_$1,_$2] do not conform to trait MyType's type parameter bounds [T,M <: Mode[T]]
reify(t)
// error: type arguments [_$1,_$2] do not conform to trait MyType's type parameter bounds [T,M <: Mode[T]]
reify("some string" -> t)
// error: type arguments [_$1,_$2] do not conform to trait MyType's type parameter bounds [T,M <: Mode[T]]
事实上,编译时的问题并不特定于元组。例如:
List(t)
// error: type arguments [_$1,_$2] do not conform to trait MyType's type parameter bounds [T,M <: Mode[T]]
我的猜测是,虽然 t 的单独定义不会触发编译错误,但对 t 的额外“触摸”可能会。如果您让编译器推断出正确的类型,一切都会正常工作:
val t2 = new MyType[String, SubMode[String]] {}
t2: MyType[String,SubMode[String]] = $anon$1@19d53ab4
"some string" -> t2
// res1: (String, MyType[String,SubMode[String]]) = (some string,$anon$1@19d53ab4)
List(t2)
// res2: List[MyType[String,SubMode[String]]] = List($anon$1@19d53ab4)