【问题标题】:How to clean up "a type was inferred to be `Any`" warning?如何清理“类型被推断为`Any`”警告?
【发布时间】:2014-06-10 22:30:47
【问题描述】:

我有以下代码:

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]                                                   ^

如何更改此代码以清除警告?

【问题讨论】:

    标签: scala warnings typing


    【解决方案1】:

    编译代码时必须有-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]
    

    【讨论】:

      【解决方案2】:

      我没有收到您的代码的警告。如果你使用 orElse 而不使用 reduce 会怎样?

      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>
      

      【讨论】:

        【解决方案3】:

        我认为这是因为 Akka 的默认 Actors 是无类型的。这意味着您传递的每条消息都是 Any 类型,并且通过模式匹配进行匹配。因此,要么输入 reduceLeft 参数,要么忽略警告。

        【讨论】:

          【解决方案4】:

          这个新警告是在 Scala 2.11 中引入的

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-10-04
            • 2021-03-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多