函数或元组等具有Scala可以根据其参数长度(arity)推断其类型的能力。
例如(1,2) is of type Tuple2
我可以使用Scala制作这样的东西吗?
我提供了我的玩具箱:
implicit class EnrichedWithToTuple[A](elements: Seq[A]) {
def toTuple2 = elements match {case Seq(a, b) => (a, b) }
def toTuple3 = elements match {case Seq(a, b, c) => (a, b, c) }
def toTuple = (*elements) // what I want is Scala infer the Tuple'N' for me by arg length.
}
val tuple = List(1, 2, 3).toTuple