varargs参数作为类型参数?

时间:2010-06-16 11:11:32

标签: scala

有没有办法创建类似的东西:

class F[A] {def apply(a: A) = println(a)}

所以我可以:

(new F[Int*])(1,2,3)

更新:但是否则,我希望F接受正常参数:

(new F[Int])(1)

1 个答案:

答案 0 :(得分:1)

scala> class F[A] { def apply(a: A*) = a.length }
defined class F    

scala> val instance = new F[Int]
instance: F[Int] = F@11a6631

scala> instance(1,2,3,4,5)
res4: Int = 5