logo       

Re: Empty Iterators: msg#00423

lang.scala

Subject: Re: Empty Iterators

Iterator[+a] is covariant in a. That means that X<:Y implies Iterator[X]<:Iterator[Y].
And since Nothing <: T for any T, you have Iterator[Nothing] <: Iterator[T] for any T you can think of, even if it is a type parameter A.

In short, having an empty iterator of type Iterator[Nothing] is just as good as having one of type Iterator[A].

hope this helps,
Burak

On 1/27/07, Miles Sabin <miles@xxxxxxxxxxxxxx> wrote:
What's the canonical way of getting an empty Iterator[A]? Iterator.empty
only wants to give me an Iterator[Nothing], which isn't quite what I'm
after.

The context is as the zero for a fold,

  class DisjointUnion[A](family : List[Set[A]])
    extends Set[A]
  {
    val size = ...

     def contains(elem: A) = ...

     def elements = family.foldLeft(???)(
       (i : Iterator[A], s : Set[A]) => i ++ s.elements)
  }

It feels like the '???' should be replaced by something like,

     def elements = family.foldLeft(Iterator.empty[A])(
       (i : Iterator[A], s : Set[A]) => i ++ s.elements)

where Iterator.empty[A] has the obvious definition,

  def empty[A] : Iterator[A] = /* old Iterator.empty renamed */

Could this be done without too much breakage elsewhere in the Scala
libraries?

Cheers,


Miles



--
Burak Emir
Research Assistant / PhD Candidate
Programming Methods Group
EPFL, 1015 Lausanne, Switzerland
http://lamp.epfl.ch/~emir
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise