如何调用Scala';排队(iter:Iterable[B])?

如何调用Scala';排队(iter:Iterable[B])?,scala,Scala,在Scala的immutable.Queue中,有两种方法都命名为enqueue: /** Creates a new queue with element added at the end * of the old queue. * * @param elem the element to insert */ def enqueue[B >: A](elem: B) = new Queue(elem :: in, out) /**

在Scala的
immutable.Queue
中,有两种方法都命名为
enqueue

  /** Creates a new queue with element added at the end
   *  of the old queue.
   *
   *  @param  elem        the element to insert
   */
  def enqueue[B >: A](elem: B) = new Queue(elem :: in, out)

  /** Returns a new queue with all elements provided by an `Iterable` object
   *  added at the end of the queue.
   *
   *  The elements are prepended in the order they are given out by the
   *  iterator.
   *
   *  @param  iter        an iterable object
   */
  def enqueue[B >: A](iter: Iterable[B]) =
    new Queue(iter.toList reverse_::: in, out)
我不知道如何解决这个歧义,并调用第二个。以下是我尝试过的:

Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_07).
Type in expressions to have them evaluated.
Type :help for more information.

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

scala> val q: Queue[Int] = Queue(1,2,3)
q: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)

scala> q.enqueue(Iterable(4))
res0: scala.collection.immutable.Queue[Any] = Queue(1, 2, 3, List(4))

scala> q.enqueue[Int](Iterable(4))
<console>:10: error: overloaded method value enqueue with alternatives:
  (iter: scala.collection.immutable.Iterable[Int])scala.collection.immutable.Queue[Int] <and>
  (elem: Int)scala.collection.immutable.Queue[Int]
 cannot be applied to (Iterable[Int])
              q.enqueue[Int](Iterable(4))
                       ^
欢迎使用Scala 2.10.0版(Java HotSpot(TM)64位服务器虚拟机,Java 1.7.007)。
键入要计算的表达式。
键入:有关详细信息的帮助。
scala>import collection.immutable.Queue
导入collection.immutable.Queue
scala>valq:Queue[Int]=队列(1,2,3)
q:scala.collection.immutable.Queue[Int]=队列(1,2,3)
scala>q.enqueue(可数(4))
res0:scala.collection.immutable.Queue[Any]=队列(1,2,3,List(4))
scala>q.enqueue[Int](Iterable(4))
:10:错误:重载的方法值与可选项排队:
(iter:scala.collection.immutable.Iterable[Int])scala.collection.immutable.Queue[Int]
(elem:Int)scala.collection.immutable.Queue[Int]
无法应用于(Iterable[Int])
q、 排队[Int](可数(4))
^
棘手,棘手

我很担心这件事。你看,你被类型误导了

scala> q.enqueue(scala.collection.Iterable(4))
res8: scala.collection.immutable.Queue[Any] = Queue(1, 2, 3, List(4))

scala> q.enqueue(scala.collection.immutable.Iterable(4))
res9: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4)

默认情况下导入的
Iterable
scala.collection.Iterable
,它可以是可变的,也可以是不可变的,但是不可变队列需要不可变的
Iterable

scala 2.13不赞成重载,而赞成
enqueueAll

同时,一种更简单的方法是明确地提到类型

  val q = Queue[Vector[Int]]()

  val uQ: Queue[Vector[Int]] = q.enqueue(Vector(6))

我得说,这对我来说是一个错误,但也许有什么我没有得到。@RandallSchulz IMHO,两件事:你确实错过了什么,但那真的是一个错误。@DanielCSobral(好久了,丹尼尔!)你想在票上要求什么?文档澄清???@RandallSchulz我认为应该更改API。没有理由需要一个不可变的iterable。我明白了,谢谢!此外,我还没有注意到Seq、IndexedSeq、Iterable和Traversable在默认情况下不是不变的。我假设一切都是不可变的,比如列表、流和向量。具有讽刺意味的是,
scala.collection.Whatever
类型只有不可变的成员补足,因为这本质上是任何给定集合类实现的可变和不可变对等体之间的共同点。