Reputation: 29463
I am trying to create a Queue in Scala by doing:
import scala.collection.immutable.Queue
val empty = new Queue[Int]
However I am getting an error stating that the Queue constructor is protected. If this is the case, am I missing something? All the Queue methods seem to be defined and working. Must I really extend the Queue class for no reason just to use a Queue?
Upvotes: 7
Views: 2870
Reputation: 36269
scala> val empty = Queue [Int]()
empty: scala.collection.immutable.Queue[Int] = Queue()
Upvotes: 0
Reputation: 297275
Use one of the factories:
scala.collection.immutable.Queue()
scala.collection.immutable.Queue.empty
Note that immutable queues are co-variant, so you usually don't need to define a type for it. One exception would be var
declarations.
Upvotes: 3
Reputation: 62855
For empty Queue use companion object:
val empty = Queue.empty[Int]
Upvotes: 15