2013-01-16 12 views
5

In Scala di immutable.Queue, ci sono due metodi sia chiamato enqueue:Come chiamare Scala Queue.enqueue (iter: Iterable [B])?

/** 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) 

non so come risolvere l'ambiguità e chiamare il secondo. Ecco cosa ho provato:

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)) 
        ^
+1

Devo dire che sembra un bug per me, ma forse c'è qualcosa che non riesco a ottenere. –

+0

@RandallSchulz IMHO, entrambe le cose: ti sei perso qualcosa, ma quello è davvero un bug. –

risposta

8

Ingannevole, difficile!

I created a ticket su di esso. Vedi, sei stato ingannato dai tipi!

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) 

Il Iterable importato di default è il scala.collection.Iterable, che può essere sia mutabile o immutabile, ma la coda immutabile richiede l'immutabileIterable invece.

+0

@DanielCSobral (Molto tempo, Daniel!) Che cosa hai intenzione di richiedere nel biglietto? Chiarificazione della documentazione ?? –

+1

@RandallSchulz Penso che l'API debba essere modificata. Non c'è motivo di richiedere un iterabile immutabile. –

+0

Vedo, grazie! Inoltre, non ho notato che Seq, IndexedSeq, Iterable e Traversable non sono immutabili per impostazione predefinita. Supponevo che tutto fosse immutabile come Elenco, Stream e Vettore. –

Problemi correlati