Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean up "a type was inferred to be `Any`" warning?

I have the following code:

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 }
}

This functions correctly when run, but it produces the following warning:

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

How can I change this code to clean up the warning?

like image 906
Greg Avatar asked Jun 10 '14 22:06

Greg


2 Answers

you must have -Xlint compiler flag when you compile the code. Looks like this warning indeed was added in 2.11.x. And it's a bug in scalac (https://issues.scala-lang.org/browse/SI-9211) as if you remove type alias (from orElse argument) it works fine

$ 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]
like image 124
Eugene Platonov Avatar answered Nov 11 '22 02:11

Eugene Platonov


I don't get a warning for your code. What if you use orElse without 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>
like image 1
Christian Avatar answered Nov 11 '22 00:11

Christian