模式匹配scala中的零参数函数:通过警告而迷惑 [英] Pattern matching zero-argument functions in scala: mystified by warning

查看:158
本文介绍了模式匹配scala中的零参数函数:通过警告而迷惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和斯卡拉的分布演员一起玩。非常好。



我有一个执行传入函数对象的服务器。
例如,客户端有

pre $对象任务{
def foo = {Console.println(我是Foo)};
def bar = {Console.println(I am Bar);}
}

//在客户端演员...
...
服务器! Tasks.foo _
...

服务器可以选择并执行它们与演员代码相似,如

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

这一切都很好(确实非常酷),但我'$ m code> scalac 为服务器代码输出一条警告消息,这让我很迷惑:

 警告:类型模式中的非变量类型参数Unit未被选中,因为它被擦除消除
case task:(()=> Unit)=>
$

如何清理此警告?



(我对 Unit >类型与()=>单元之间的差异相当不清楚。 code>类型的零参数函数只是试图在 react 中匹配 task:Unit
$ b

在Debian上使用Scala 2.7.5,使用Sun的Java6。



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

由于擦除,Unit在运行时不可见。
如果你真的不关心返回类型,你可以在反应块中做到这一点:

  case任务:Function0 [_] =>任务()


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.

解决方案

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()

这篇关于模式匹配scala中的零参数函数:通过警告而迷惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆