简体   繁体   English

“new”关键字不适用于不可变队列

[英]“new” Keyword doesn't work with immutable Queue

I tried to create a Queue with new Keyword..I did it for both mutable and immutable Queue. 我尝试用新的Keyword创建一个Queue ..我为可变和不可变的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 ??? 谁能告诉我,为什么会这样?

From a quick look at the sourcecode , I would venture that the immutable version is essentially implemented through a couple of List s which pivots as needed to give nice performance between reads and writes (queue/dequeue). 从快速查看源代码开始 ,我冒昧地认为不可变版本基本上是通过一些List的,这些List根据需要进行调整,以便在读取写入 (队列/出队)之间提供良好的性能。

This is quite dissimilar from the other collections, in that it needs both mentioned List s as parameters to the class constructor. 这与其他集合非常不同,因为它需要将List s作为class构造函数的参数。
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. 另一方面,伴随对象通过接受Queue内容的可变数量的初始值,提供与其他集合一致的公共工厂方法。

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 . 缺少的是类的公共构造函数,它将模仿伴随对象apply调用,获取初始值,并使用它们构建“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. 也许这不是必要的,或者它是一种疏忽,或者是一个我无法弄清楚的更深层次的问题。

The compiler already tells you why it doesn't work, because the conctructor is protected . 编译器已经告诉你它为什么不起作用,因为结构符号是protected You have to use the apply method of the companion to create an immutable.Queue : 您必须使用随immutable.Queueapply方法来创建immutable.Queue

val queue = immutable.Queue(1,2,3)

I'll interpret the question as "why is the constructor protected". 我将这个问题解释为“为什么构造函数受到保护”。 We can only guess, but I tend to think it is a simple oversight. 我们只能猜测,但我倾向于认为这是一个简单的疏忽。 As a matter of style it is often better to use the companion objects to instantiate collections, but given that you can also directly instantiate other concrete collections, this should be doble with Queue . 作为一种风格问题,通常最好使用伴随对象来实例化集合,但考虑到您也可以直接实例化其他具体集合,这应该与Queue In other words, I definitely think that Queue should have this no arg contructor to create an empty queue, if only as a matter of consistency: 换句话说,我绝对认为Queue应该有这个没有arg构造器来创建一个空队列,如果只是为了一致性:

class Queue[+A] extends ... {
  ...
  def this() { this( Nil, Nil ) }
  ...
}

It is very subjective though. 但这是非常主观的。

I would guess that because it is encouraged to use empty to create void immutable collection, as it avoids overhead (if you do this multiple times) and exploit the immutability most. 我猜这是因为鼓励使用empty来创建void不可变集合,因为它避免了开销(如果你多次这样做)并且最大限度地利用不变性。 Just no need to create new instance every single time, because "there is only one empty queue in the world". 不需要每次都创建新实例,因为“世界上只有一个空队列”。 For the mutable collection, it is obvious that this does not work. 对于可变集合,很明显这不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM