logo       

Re: Empty Iterators: msg#00425

lang.scala

Subject: Re: Empty Iterators

Burak Emir wrote,
> 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].

I don't think it is in the example I gave,

class DisjointUnion[A](family : List[Set[A]])
extends Set[A]
{
def elements =
family.foldLeft(Iterator.empty)((i, s) => i ++ s.elements)
}

gives me the compiler error,

type mismatch;
found : scala.Iterator[A]
required: scala.Iterator[scala.Nothing]

I could do this,

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

but that's pretty ugly. Or I can add the following,

object Collections
{
def emptyIterator[A] : Iterator[A] = Iterator.empty
}

and do this,

def elements =
family.foldLeft(Collections.emptyIterator[A])(
(i, s) => i ++ s.elements)

but it'd nicer if emptyIterator (or something equivalent) lived on
scala.Iterator.

Or am I missing something?

Cheers,


Miles



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise