Using the apply
method of the companion object is the right way to do it, but you could also add a type parameter on apply
itself:
object Queue {
def apply[T]() = new Queue[T]
}
So that you can create a Queue
of the right type:
val q = Queue[Int]()
Usually you also allow populating the sequence on creation, so that the element type can be inferred, as in:
def apply[T](elms: T*) = ???
So that you can do:
val q = Queue(1,2,3) // q is a Queue[Int]