Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created February 14, 2012 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeno-by/1831018 to your computer and use it in GitHub Desktop.
Save xeno-by/1831018 to your computer and use it in GitHub Desktop.
object Test extends App {
def foo(bar: Any) = bar
val code = foo{
object lazyLib {
def delay[A](value: => A): Susp[A] = new SuspImpl[A](value)
implicit def force[A](s: Susp[A]): A = s()
abstract class Susp[+A] extends Function0[A]
class SuspImpl[A](lazyValue: => A) extends Susp[A] {
private var maybeValue: Option[A] = None
override def apply() = maybeValue match {
case None =>
val value = lazyValue
maybeValue = Some(value)
value
case Some(value) =>
value
}
}
}
import lazyLib._
val s: Susp[Int] = delay { println("evaluating..."); 3 }
println("2 + s = " + (2 + s)) // implicit call to force()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment