18

As an exercise, I tried to create an implicit conversion that would accept a function and produce a Runnable. That way you could call Java methods that accept Runnable objects and use them like closures.

The implicit conversion is easy enough:

    implicit def funToRunnable(fun : Unit) = new Runnable() { def run = fun }

However I don't know how to call it. How do you pass in a no-arg function that returns Unit, without having it be evaluated at once? For example, I'd like the following to print "12" but instead it prints "21" because print("2") is evaluated at once.

    var savedFun : Runnable = null
    def save(r : Runnable) = { savedFun = r }

    save(print("2"))
    print("1")
    savedFun.run()

How do I tell the compiler to treat print("2") as the body of a function, not something to be evaluated at once? Some possibilities I tried, such as

    save(() => print("2"))

or

    save(=> print("2"))

are not legal syntax.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Patrick Arnesen
  • 1,138
  • 1
  • 9
  • 13
  • you meant *"call java methods that accept Runnables and pass functions into them like closures"*? because methods that accept other functions are not called closures; the functions they accept are called (sometimes) closures. – Erik Kaplun Mar 31 '14 at 02:38

5 Answers5

24

arg, just answered my own question. I implemented the implicit conversion incorrectly. The correct implementation is

implicit def funToRunnable(fun: () => Unit) = new Runnable() { def run() = fun() }

and you call it like this:

save(() => print("2"))

This will yield "2"

smddzcy
  • 443
  • 4
  • 19
Patrick Arnesen
  • 1,138
  • 1
  • 9
  • 13
  • I was on the same track. Do you have any idea on why `def run = run` doesn't work? – OscarRyz Jun 18 '10 at 23:49
  • 2
    `def run = run` will always be an infinite recursion. Even if there's a `run` defined in an enclosing scope, the one defined by this `def` will shadow it, guaranteeing a direct, unconditional recursive call. – Randall Schulz Jun 19 '10 at 01:27
  • I meant `def run = fun` without parens – OscarRyz Jun 20 '10 at 05:00
  • 2
    @Support because fun without parentheses just creates a method that returns the function that was passed. To call a function type, you have to use parentheses, otherwise you're just returning the funciton itself. – Ken Bloom Jun 22 '10 at 14:03
  • 5
    Shouldnt it yield 2 instead of 12? – David Frank Sep 04 '13 at 08:05
14

If you wanted to live dangerously, you could convert anything to a runnable:

implicit def whateverToRunnable[F](f: => F) = new Runnable() { def run() { f } }

scala> val t = new Thread(println("Hello"))
t: java.lang.Thread = Thread[Thread-2,5,main]

scala> t.start()
Hello

Or you could create your own thread-creator-and-starter:

def thread[F](f: => F) = (new Thread( new Runnable() { def run() { f } } )).start

scala> thread { println("Hi"); Thread.sleep(1000); println("Still here!") }
Hi

scala> Still here!

If you wanted to return the thread, then

def thread[F](f: => F) = {
  val t = new Thread( new Runnable() { def run() { f } } )
  t.start()
  t
}

But all of this, while useful, is perhaps even less useful than scala.actors.Futures (tested only on 2.8):

scala> import scala.actors.Futures

scala> val x = Futures.future { Thread.sleep(10000); "Done!" }
x: scala.actors.Future[java.lang.String] = <function0>

scala> x.isSet
res0: Boolean = false

scala> x.isSet
res1: Boolean = false

scala> x()   // Waits until the result is ready....
res2: java.lang.String = Done!
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
5

Actually, you can do it even nicer with call-by-name argument:

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

Usage:

import concurrent.ExecutionContext.Implicits.global._
execute(print("hello"))
doughting
  • 382
  • 4
  • 10
5

Interesting, this way you can execute code that receives a Runnable and pass it a closure.

See:

scala> new Thread( ()  => print( "Hello" ) ).start()
<console>:5: error: overloaded method constructor Thread with alternatives (java.lang.ThreadGroup,java.lang.Runnable,java.lang.String,Long)java.lang.Thread <and> (java.lang.ThreadGroup,java.lang.Runnable,java.lang.String)java.lang.Thread <and> (java.lang.Runnable,java.lang.String)java.lang.Thread <and> (java.lang.ThreadGroup,java.lang.String)java.lang.Thread <and> (java.lang.String)ja...
       new Thread( ()  => print( "Hello" ) ).start()


scala> implicit def funcToRunnable( func : () => Unit ) = new Runnable(){ def run() = func() }
funcToRunnable: (() => Unit)java.lang.Object with java.lang.Runnable

scala> def doRun( runnable: Runnable ) = runnable.run
doRun: (Runnable)Unit

scala> doRun( () => print("Hola"))
Hola

scala> new Thread(()=>print("Hello")).start()

scala> Hello
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

Yet another way to run some code in a different thread:

scala.actors.Actor.actor { ...doSomething()... }
  • Runnable is not only used to run stuff in a newly Threads. For example in GUI development it is used to run stuff on the special UI thread. – Martin May 24 '12 at 09:09