How to clear "type has been deduced for warning" Any "?

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 works correctly at startup, but raises 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 clear the warning?

+6
source share
4 answers

I do not get a warning for your code. What if you use orElse without shorthand?

 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> 
0
source

you must have the -Xlint flag of the compiler when compiling the code. Looks like this warning was indeed added in 2.11.x. And this is a mistake in scalac ( https://issues.scala-lang.org/browse/SI-9211 ), as if you are removing a type alias (from the 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] 
+2
source

I think this is because Akka's default actors are untyped. This means that every message you send is of type Any, and does not match patterns matching. Therefore, either you add the type to reduceLeft arguments, or you simply ignore the warning.

0
source

This new warning was introduced with Scala 2.11.

0
source

Source: https://habr.com/ru/post/970651/


All Articles