我有以下代码:
class TestActor() extends RootsActor() {
// Receive is a type resolving to PartialFunction[Any, Unit]
def rec2 : Actor.Receive = {
case "ping" => println("Ping received!!!")
}
def recAll = List(super.receive, rec2)
// Compose parent class' receive behavior with this class' receive
override def receive = recAll.reduceLeft { (a,b) => a orElse b }
}
运行时此功能正常,但会产生以下警告:
[warn] /Users/me/git/proj/roots/src/multi-jvm/scala/stuff/TestActor.scala:18: a type was inferred to be `Any`; this may indicate a programming error.
[warn] override def receive = recAll.reduceLeft { (a,b) => a orElse b }
[warn] ^
如何更改此代码以清除警告?
答案 0 :(得分:1)
我没有收到代码警告。如果你使用orElse而不减少怎么办?
scala> import akka.actor._
import akka.actor._
scala> class RootActor extends Actor { def receive = { case _ => println("bang") }}
defined class RootActor
scala> class TestActor extends RootActor {
| def rec2: Actor.Receive = { case "ping" => println("ping") }
| override def receive = super.receive orElse rec2
| }
defined class TestActor
scala>
答案 1 :(得分:0)
我认为这是因为Akka的默认Actors是无类型的。这意味着您传递的每条消息都是Any类型,而不是模式匹配。所以你要么把类型放在reduceLeft参数中,要么忽略警告。
答案 2 :(得分:0)
Scala 2.11引入了这个新警告