Scala: Runnable and Callable

Scala functions don’t implement the Runnable and Callable interfaces even though there’s a simply mapping. Luckily, Scala provides us with a mechanism to extend functions (and other classes) on our own: implicits conversions. Here are two usable definitions:


implicit def runnable(f: () => Unit): Runnable =
  new Runnable() { def run() = f() }


import java.util.concurrent.Callable

implicit def callable[T](f: () => T): Callable[T] =
  new Callable[T]() { def call() = f() }