I have a case class, taking a Seq[T] as parameter:
case class MyClass(value: Seq[T])
I now want to be able to write
MyClass(t1,t2,t3)
So I defined
object MyClass {
def apply(value: T*) = new MyClass(value.toSeq)
}
It doesn’t work, because the case class defines
object MyClass {
def apply(value: Seq[T])
}
and Seq[T] and T* have the same type after erasure, so I can’t overload them.
But I’d like to allow both access ways. Both ways should be allowed:
MyClass(t1,t2,t3)
MyClass(some_seq_of_T)
Since Seq[T] and T* are almost the same type (at least after erasure; and inside of the function having the parameter T* becomes Seq[T]), I think there should be a way to allow both ways of calling it.
Is there?
You could cheat a little bit and define your companion like this:
The first
applyhandles theMyClass(1,2,3)as long as there is at least one argument.The scond
applyhandles the 0-argument case. They don’t conflict with the regular constructor.This way you can write
MyClass(Seq(1,2,3)),MyClass(1,2,3), andMyClass(). Just note that for the empty one you’ll have to tell it what type to return, or else it will assume aMyClass[Nothing]