Mam następujący kod Scala.Scala pattern matching confusion with Option [Any]
import scala.actors.Actor
object Alice extends Actor {
this.start
def act{
loop{
react {
case "Hello" => sender ! "Hi"
case i:Int => sender ! 0
}
}
}
}
object Test {
def test = {
(Alice !? (100, "Hello")) match {
case i:Some[Int] => println ("Int received "+i)
case s:Some[String] => println ("String received "+s)
case _ =>
}
(Alice !? (100, 1)) match {
case i:Some[Int] => println ("Int received "+i)
case s:Some[String] => println ("String received "+s)
case _ =>
}
}
}
Po wykonaniu Test.test
, pojawia się komunikat:
scala> Test.test
Int received Some(Hi)
Int received Some(0)
ja spodziewałem wyjście
String received Some(Hi)
Int received Some(0)
Jakie jest wytłumaczenie?
Jako drugie pytanie, mam unchecked
ostrzeżenia z powyższym następująco:
C:\scalac -unchecked a.scala
a.scala:17: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
case i:Some[Int] => println ("Int received "+i)
^
a.scala:18: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
case s:Some[String] => println ("String received "+s)
^
a.scala:22: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
case i:Some[Int] => println ("Int received "+i)
^
a.scala:23: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
case s:Some[String] => println ("String received "+s)
^
four warnings found
Jak mogę uniknąć ostrzeżenia?
EDYCJA: Dziękujemy za sugestie. pomysł Daniela jest ładny, ale nie wydaje się, aby pracować z typów generycznych, jak w poniższym przykładzie
def test[T] = (Alice !? (100, "Hello")) match {
case Some(i: Int) => println ("Int received "+i)
case Some(t: T) => println ("T received ")
case _ =>
}
Poniższy
błędu
ostrzegawczy napotkanego: warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure
Dzięki! Inne odpowiedzi też są dobre, w tym obejście sugerowane przez Kevina. Ale wydaje się to najbardziej eleganckim sposobem na naprawienie mojego kodu bez konieczności wielokrotnego przepisywania. – Jus12
Czy możesz zaproponować podobne rozwiązanie dla typów ogólnych? jak w: 'test def [T] = (Alicja!? (100," Hello ")) dopasuj {case Some (t: T) => println (" T received "); case _ => println ("coś innego otrzymanego")} ' – Jus12
@ Jus12 W ten sposób nie będzie działać. Będziesz musiał uzyskać 'm: Manifest [T]', a następnie coś w stylu 'case Some (t: T) if m.erasure.isAssignableFrom (t.getClass()) =>'. –