Skip to content

Instantly share code, notes, and snippets.

@ryandavidhartman
Created February 25, 2019 01:08
Show Gist options
  • Save ryandavidhartman/144d08a93e8213786888459c5c9153d2 to your computer and use it in GitHub Desktop.
Save ryandavidhartman/144d08a93e8213786888459c5c9153d2 to your computer and use it in GitHub Desktop.
// Traditional callback (using a continuation)
def program(a: A, k: B => Unit): Unit
// Let's start moving this towards a Future
def program(a: A)(B => Unit): Unit
// now introduce the following type alias
type Future[+T] = (T => Unit) => Unit
def program(a: A): Future[B]
// we could do a better implemetation of Future as
type Future[+T] = (Try[T] => Unit) => Unit
// we could make this closer to the real Scala Future by
// reifying the type alias into a Trait
trait Future[+T] extends (Try[T] => Unit) => Unit {
def apply(k: Try[T] => Unit): Unit
}
// now just renaming 'apply' to 'onComplete' and this is
// close to the real definition of a Future
trait Future[+T] extends (Try[T] => Unit) => Unit {
def onComplete(k: Try[T] => Unit): Unit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment