返回同源元组的函数的方法签名?

时间:2014-02-22 01:05:30

标签: scala types tuples

忽略列表是一种同质元组的想法:

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)
                                        ^

有没有办法利用类型系统来强制实现同质性?

1 个答案:

答案 0 :(得分:2)

object Test {
  type HomogeneousPair[T] = Tuple2[T, T]

  val x: HomogeneousPair[Int] = (1, 2)
}