[英]“new” Keyword doesn't work with immutable Queue
我尝试用新的Keyword创建一个Queue ..我为可变和不可变的Queue做了它。
但是当我尝试使用不可变队列时,它会出错:
<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]()
但是当我尝试使用可变队列,不可变堆栈,可变堆栈的代码时...它运行良好
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()
谁能告诉我,为什么会这样?
从快速查看源代码开始 ,我冒昧地认为不可变版本基本上是通过一些List
的,这些List
根据需要进行调整,以便在读取和写入 (队列/出队)之间提供良好的性能。
这与其他集合非常不同,因为它需要将List
s作为class
构造函数的参数。
另一方面,伴随对象通过接受Queue
内容的可变数量的初始值,提供与其他集合一致的公共工厂方法。
缺少的是类的公共构造函数,它将模仿伴随对象apply
调用,获取初始值,并使用它们构建“enque / dequeue” Lists
。
也许这不是必要的,或者它是一种疏忽,或者是一个我无法弄清楚的更深层次的问题。
编译器已经告诉你它为什么不起作用,因为结构符号是protected
。 您必须使用随immutable.Queue
的apply
方法来创建immutable.Queue
:
val queue = immutable.Queue(1,2,3)
我将这个问题解释为“为什么构造函数受到保护”。 我们只能猜测,但我倾向于认为这是一个简单的疏忽。 作为一种风格问题,通常最好使用伴随对象来实例化集合,但考虑到您也可以直接实例化其他具体集合,这应该与Queue
。 换句话说,我绝对认为Queue
应该有这个没有arg构造器来创建一个空队列,如果只是为了一致性:
class Queue[+A] extends ... {
...
def this() { this( Nil, Nil ) }
...
}
但这是非常主观的。
我猜这是因为鼓励使用empty
来创建void不可变集合,因为它避免了开销(如果你多次这样做)并且最大限度地利用不变性。 不需要每次都创建新实例,因为“世界上只有一个空队列”。 对于可变集合,很明显这不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.