忽略列表是一种同质元组的想法:
scala> def homogeneousPair[T]: (T, T) = (1, 2)
<console>:7: error: type mismatch;
found : Int(1)
required: T
def homogeneousPair[T]: (T, T) = (1, 2)
^
<console>:7: error: type mismatch;
found : Int(2)
required: T
def homogeneousPair[T]: (T, T) = (1, 2)
^
有没有办法利用类型系统来强制实现同质性?
答案 0 :(得分:2)
object Test {
type HomogeneousPair[T] = Tuple2[T, T]
val x: HomogeneousPair[Int] = (1, 2)
}