scala - 如何清理 "a type was inferred to be ` Any`"警告?

标签 scala warnings typing

我有以下代码:

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/

相关文章:

c# - 如何在 C# 中创建自定义对象类型类?

java - 你如何在 Play Framework scala 模板中使用 DTO?

斯卡拉 future 。如果其中一个失败则采取行动

c++ - 其他库中的编译器警告

regex - 如何在 Typescript 中定义正则表达式匹配的字符串类型?

typescript - 为什么使用 Typescript - Angular2 进行类型定义 (.d.ts)?

scala - Spark 应用程序conf中的setMaster与sparkSubmit上的--master标志之间的差异或冲突

scala - 如果我将 Option 传递给它,Scala for-yield 可以返回 None 吗?

ruby - 已经初始化的常量警告

xcode - 如何在 XIB 文件中抑制 Xcode "Attribute Unavailable"警告?