Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala pattern matching confusion with Option[Any]

I have the following Scala code.

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

After doing Test.test, I get the output:

scala> Test.test
Int received Some(Hi)
Int received Some(0)

I was expecting the output

String received Some(Hi)
Int received Some(0)

What is the explanation?

As a second question, I get unchecked warnings with the above as follows:

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

How can I avoid the warnings?

EDIT: Thanks for the suggestions. Daniel's idea is nice but does not seem to work with generic types, as in the example below

def test[T] = (Alice !? (100, "Hello")) match { 
   case Some(i: Int) => println ("Int received "+i) 
   case Some(t: T) => println ("T received ") 
   case _ =>  
}

The following error warning is encountered: warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure

like image 761
Jus12 Avatar asked Sep 24 '10 15:09

Jus12


People also ask

How do you match a pattern in Scala?

A pattern match includes a sequence of alternatives, each starting with the keyword case. Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches. An arrow symbol => separates the pattern from the expressions.

Does Scala have pattern matching?

Notes. Scala's pattern matching statement is most useful for matching on algebraic types expressed via case classes. Scala also allows the definition of patterns independently of case classes, using unapply methods in extractor objects.

What is the use of Scala pattern matching?

Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C.

What is some () in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.


3 Answers

This is due to type-erasure. The JVM does not know of any type parameter, except on arrays. Because of that, Scala code can't check whether an Option is an Option[Int] or an Option[String] -- that information has been erased.

You could fix your code this way, though:

object Test {
  def test = {
    (Alice !? (100, "Hello")) match {
      case Some(i: Int) => println ("Int received "+i)
      case Some(s: String) => println ("String received "+s)
      case _ =>
    }
    (Alice !? (100, 1)) match {
      case Some(i: Int) => println ("Int received "+i)
      case Some(s: String) => println ("String received "+s)
      case _ =>
    }
  }
}

This way you are not testing what the type of Option is, but what the type of its contents are -- assuming there is any content. A None will fall through to the default case.

like image 58
Daniel C. Sobral Avatar answered Oct 06 '22 04:10

Daniel C. Sobral


Any information about type parameters is only available at compile-time, not at runtime (this is known as type erasure). This means that at runtime there is no difference between Option[String] and Option[Int], so any pattern matching on the type Option[String], will also match Option[Int] because at runtime both is just Option.

Since this is almost always not what you intend, you get a warning. The only way to avoid the warning is not to check the generic type of something at runtime (which is fine because that doesn't work like you want it to anyway).

There is no way to check whether an Option is an Option[Int] or an Option[String] at runtime (other than inspecting the content if it's a Some).

like image 27
sepp2k Avatar answered Oct 06 '22 05:10

sepp2k


As already stated, you're up against erasure here.

For the solution... It's normal with Scala actor to define case classes for each message type you're likely to send:

case class MessageTypeA(s : String)
case class MessageTypeB(i : Int)

object Alice extends Actor {
  this.start
  def act{
    loop{
      react {
        case "Hello" => sender ! MessageTypeA("Hi")
        case i:Int => sender ! MessageTypeB(0)
      }
    }
  }
}
object Test {
  def test = {
    (Alice !? (100, "Hello")) match {
      case Some(MessageTypeB(i)) => println ("Int received "+i)
      case Some(MessageTypeA(s)) => println ("String received "+s)
      case _ =>
    }
    (Alice !? (100, 1)) match {
      case Some(MessageTypeB(i)) => println ("Int received " + i)
      case Some(MessageTypeA(s)) => println ("String received " + s)
      case _ =>
    }
  }
}
like image 20
Kevin Wright Avatar answered Oct 06 '22 03:10

Kevin Wright