IttayD
IttayD

Reputation: 29123

varargs parameter as type parameter?

Is there any way to create something similar to this:

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

So that I can:

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

UPDATE: but otherwise, I want F to accept normal parameters:

(new F[Int])(1)

Upvotes: 2

Views: 209

Answers (1)

michael.kebe
michael.kebe

Reputation: 11085

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

Upvotes: 1

Related Questions