Creating a Queue in Scala
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?
For empty Queue use companion object:
val empty = Queue.empty[Int]
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.
scala> val empty = Queue [Int]()
empty: scala.collection.immutable.Queue[Int] = Queue()
精彩评论