2014-06-10 10 views
6

Ho il seguente codice:Come ripulire "un tipo è stato dedotto come avviso" Qualsiasi "?

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

Questo funziona correttamente quando viene eseguito, ma produce il seguente avviso:

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

Come posso cambiare questo codice per ripulire l'avvertimento?

risposta

0

Non ricevo un avviso per il codice. Cosa succede se usi oElse senza ridurre?

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

Penso che questo sia dovuto al fatto che gli attori predefiniti di Akka non sono stati tipizzati. Ciò significa che ogni messaggio che passi è di tipo Any e che è abbinato alla corrispondenza del pattern. Quindi, inserisci gli argomenti di riduzione in Left o semplicemente ignora l'avviso.

0

Questo nuovo avvertimento è stato introdotto con Scala 2.11

2

è necessario disporre di -Xlint compilatore flag quando si compila il codice. Sembra che questo avviso sia stato aggiunto in 2.11.x. Ed è un bug in scalac (https://issues.scala-lang.org/browse/SI-9211) come se si rimuovesse il tipo alias (dall'argomento oElse) funziona bene

$ 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] 
Problemi correlati