加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何调用Scala的Queue.enqueue(iter:Iterable [B])?

发布时间:2020-12-16 09:49:04 所属栏目:安全 来源:网络整理
导读:在 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
在 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,3)

scala> q.enqueue(Iterable(4))
res0: scala.collection.immutable.Queue[Any] = Queue(1,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))
                       ^

解决方法

棘手,棘手!

我created a ticket关于它.你看,你被类型误导了!

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读