Why does Option not extend the Iterable trait directly?

Posted by oxbow_lakes on Stack Overflow See other posts from Stack Overflow or by oxbow_lakes
Published on 2010-04-09T11:53:38Z Indexed on 2010/04/11 7:13 UTC
Read the original article Hit count: 189

Filed under:
|
|

Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly:

def iterator = new Iterator[A] {
  var end = !isDefined
  def next() = {
    val n = if (end) throw new NoSuchElementException() else get
    end = true
    n
  }

  def hasNext = !end
}

EDIT: In fact it's even weider than that because in 2.8 Option does declare an iterator method:

def iterator: Iterator[A] = 
  if (isEmpty) Iterator.empty else Iterator.single(this.get)

© Stack Overflow or respective owner

Related posts about scala

Related posts about option