Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"new" Keyword doesn't work with immutable Queue

Tags:

stack

scala

queue

I tried to create a Queue with new Keyword..I did it for both mutable and immutable Queue.

But when I tried with immutable Queue, it gives error :

<console>:8: error: constructor Queue in class Queue cannot be accessed in object $iw
 Access to protected constructor Queue not permitted because
 enclosing class object $iw in object $iw is not a subclass of 
 class Queue in package immutable where target is defined
       val a=new Queue[Int]()
             ^

scala> import scala.collection.immutable.Queue
import scala.collection.immutable.Queue

scala> val a=new Queue[Int]()
<console>:8: error: constructor Queue in class Queue cannot be accessed in object $iw
 Access to protected constructor Queue not permitted because
 enclosing class object $iw in object $iw is not a subclass of 
 class Queue in package immutable where target is defined
       val a=new Queue[Int]()

But when I tried this code with mutable Queue, immutable Stack, mutable Stack...It works well

scala> import scala.collection.mutable.Queue
import scala.collection.mutable.Queue

scala> val a=new Queue[Int]()
a: scala.collection.mutable.Queue[Int] = Queue()



scala> import scala.collection.immutable.Stack
import scala.collection.immutable.Stack

scala> val a=new Stack[Int]()
a: scala.collection.immutable.Stack[Int] = Stack()


scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack

scala> val a=new Stack[Int]()
a: scala.collection.mutable.Stack[Int] = Stack()

Can anyone tell me, why is it so ???

like image 758
Rishi Avatar asked Feb 18 '23 15:02

Rishi


1 Answers

From a quick look at the sourcecode, I would venture that the immutable version is essentially implemented through a couple of Lists which pivots as needed to give nice performance between reads and writes (queue/dequeue).

This is quite dissimilar from the other collections, in that it needs both mentioned Lists as parameters to the class constructor.
The companion object, on the other side, provides a public factory method that is consistent with other collections, by accepting a variable number of initial values for the Queue content.

What is missing is a public constructor for the class, that would mimic the companion-object apply call, by taking the initial values, and using them to build the "enque/dequeue" Lists.

Maybe it was not deemed essential, or it's been an oversight, or there is a deeper issue that I can't figure out.

like image 82
pagoda_5b Avatar answered Feb 27 '23 22:02

pagoda_5b