R – Pattern matching zero-argument functions in scala: thestified by warning

functional programmingpattern matchingscala

I'm playing with scala's distributed actors. Very nice.

I have a server which executes incoming function objects.
For example, the client has

object Tasks {
  def foo = {Console.println("I am Foo")};
  def bar = {Console.println("I am Bar");}
}

// In client actor...
...
  server ! Tasks.foo _
...

And the server can pick these up and execute them with actor code like

react {
  case task:(()=>Unit) =>
    task()

This all works nicely (which is very very cool indeed) but I'm mystified by a warning message output by scalac for the server code:

warning: non variable type-argument Unit in type pattern is unchecked since it is eliminated by erasure
        case task:(()=>Unit) =>
                     ^

How can I clean this warning up ?

(I'm pretty unclear on the difference between the Unit type, and the ()=>Unit type of zero-argument functions. Just trying to match task:Unit in the react is warning-free, but actually doesn't match the incoming tasks.)

Using Scala 2.7.5 on Debian, with Sun's Java6.

Best Answer

You are really matching this:

case task:Function0[Unit] => task()

Due to erasure, Unit is not visible at runtime. If you really don't care about the return type, you can do this in the react block:

case task:Function0[_] => task()