3

I'm reading the source code of Iteratee.scala: https://github.com/playframework/Play20/blob/master/framework/src/iteratees/src/main/scala/play/api/libs/iteratee/Iteratee.scala

Specifically the convenience method for constructing the "fold" iteratee.

def fold[E, A](state: A)(f: (A, E) => A): Iteratee[E, A] = {
  def step(s: A)(i: Input[E]): Iteratee[E, A] = i match {

    case Input.EOF => Done(s, Input.EOF)
    case Input.Empty => Cont[E, A](i => step(s)(i))
    case Input.El(e) => { val s1 = f(s, e); Cont[E, A](i => step(s1)(i)) }
  }
  (Cont[E, A](i => step(state)(i)))
}  

On each of the case statements, we are calling Done or Cont constructors. But where are these constructors defined? I infer that these must be implementors of the Iteratee trait but I couldn't find them by doing ctrl+F for "extends Iteratee."

Mark
  • 5,286
  • 5
  • 42
  • 73

2 Answers2

4

See docementation or source:

object Done {
/**
* Create an [[play.api.libs.iteratee.Iteratee]] in the “done” state.
* @param a Result
* @param e Remaining unused input
*/
  def apply[E, A](a: A, e: Input[E] = Input.Empty): Iteratee[E, A] = new Iteratee[E, A] {
    def fold[B](folder: Step[E, A] => Future[B]): Future[B] = folder(Step.Done(a, e))
  }
}

It's not a constructor. Done(s, Input.EOF) means Done.apply(s, Input.EOF). Same with Cont.

Community
  • 1
  • 1
senia
  • 37,745
  • 4
  • 88
  • 129
  • Oh, it's a singleton method! Confusing that they're trying to disguise it as a class though. Thanks! – Mark Dec 15 '12 at 20:34
  • Call of constructor should starts with `new`. – senia Dec 15 '12 at 20:37
  • @Mark: As a warning, these aren't _constructors_ in the Java sense, but they are in the ADT sense, and you'll often see references to e.g. "the `Done` constructor" that mean exactly what you see here. – Travis Brown Dec 15 '12 at 20:58
  • 1
    @TravisBrown: Not in this case though, because Iteratee is not a composite of type. Right? – Mark Dec 15 '12 at 21:34
-1

Done doesn't need to directly extend Iteratee. It could extend another class or trait which then extends Iteratee. You could search for class Cont and then follow the inheritance relationship from there.

helium
  • 1,077
  • 1
  • 9
  • 15