providence
providence

Reputation: 29463

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?

Upvotes: 7

Views: 2870

Answers (3)

user unknown
user unknown

Reputation: 36269

scala> val empty = Queue [Int]()
empty: scala.collection.immutable.Queue[Int] = Queue()

Upvotes: 0

Daniel C. Sobral
Daniel C. Sobral

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

om-nom-nom
om-nom-nom

Reputation: 62855

For empty Queue use companion object:

val empty = Queue.empty[Int]

Upvotes: 15

Related Questions