Matching null arguments in scala: mystified by warning

I play with scala distributed actors. Very nice.

I have a server that performs incoming functions. For example, a customer 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 select them and execute with the actor code, for example

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

All this works fine (which is very cool), but I am amazed at the warning displayed scalacfor 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 clear this warning?

(I don’t quite understand the difference between the type Unitand the ()=>Unittype of functions with a null argument. Just an attempt to map task:Unitin reactwithout warning, but actually does not match incoming tasks.)

Using scala 2.7.5 on Debian with Sun Java6.

+5
2

:

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

- . , :

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

@Mitch Blevins, .

. Scala? , ? , , (Function0[T],Manifest[T]) . , Scala , T, matchFunction(foo _).

scala> def foo = {Console.println("I am Foo")}
foo: Unit

scala> import scala.reflect.Manifest
import scala.reflect.Manifest

scala> def matchFunction[T](f: Function0[T])(implicit m : Manifest[T]) {
     |   (m,f) match {
     |     case (om: Manifest[_],of: Function0[_]) =>
     |       if(om <:< m) {
     |         of.asInstanceOf[Function0[T]]()
     |       }
     |   }
     | }
matchFunction: [T](() => T)(implicit scala.reflect.Manifest[T])Unit

scala> matchFunction(foo _)
I am Foo
+3

All Articles