我有以下代码:
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] ^
如何更改此代码以清除警告?
最佳答案
你必须有 -Xlint
编译代码时的编译器标志。看起来这个警告确实是在 2.11.x 中添加的。这是 scalac ( https://issues.scala-lang.org/browse/SI-9211 ) 中的一个错误,就好像您删除了类型别名(从 orElse 参数中)它工作正常
$ scala -Xlint
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_80).
Type in expressions to have them evaluated.
Type :help for more information.
scala> type Problem = PartialFunction[Any,Unit]
defined type alias Problem
scala> def moreProblems(problem1: Problem, problem2: Problem) = problem1 orElse problem2
<console>:8: warning: a type was inferred to be `Any`; this may indicate a programming error.
def moreProblems(problem1: Problem, problem2: Problem) = problem1 orElse problem2
^
moreProblems: (problem1: Problem, problem2: Problem)PartialFunction[Any,Unit]
scala> def moreProblems(problem1: PartialFunction[Any, Unit], problem2: Problem) = problem1 orElse problem2
<console>:8: warning: a type was inferred to be `Any`; this may indicate a programming error.
def moreProblems(problem1: PartialFunction[Any, Unit], problem2: Problem) = problem1 orElse problem2
^
moreProblems: (problem1: PartialFunction[Any,Unit], problem2: Problem)PartialFunction[Any,Unit]
scala> def moreProblems(problem1: Problem, problem2: PartialFunction[Any, Unit]) = problem1 orElse problem2
moreProblems: (problem1: Problem, problem2: PartialFunction[Any,Unit])PartialFunction[Any,Unit]
关于scala - 如何清理 "a type was inferred to be ` Any`"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24151919/