且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Scala中包含将元组转换为列表的任何方法吗?

更新时间:2022-12-16 10:46:16

适用于任何元组(scala 2.8):

Works with any tuple (scala 2.8):

myTuple.productIterator.toList

Scala 2.7:

Scala 2.7:

(0 to (myTuple.productArity-1)).map(myTuple.productElement(_)).toList


不知道如何维护常规Product或Tuple的类型信息,而对于Tuple2:


Not sure how to maintain type info for a general Product or Tuple, but for Tuple2:

def tuple2ToList[T](t: (T,T)): List[T] = List(t._1, t._2)

您当然可以为所有元组(最多22个)定义类似的类型安全的转换.

You could, of course, define similar type-safe conversions for all the Tuples (up to 22).