Scala wont pattern match with java.lang.String and Case Class

Posted by Stefan on Stack Overflow See other posts from Stack Overflow or by Stefan
Published on 2010-04-16T09:53:52Z Indexed on 2010/04/16 10:43 UTC
Read the original article Hit count: 354

Hello fellow Scala Programmers

I have been working with Scala for some month now, however I have a problem with some properly basic stuff, I am hoping you will help my out with it.

case class PersonClass(name: String, age: Int)

object CaseTester {
def main(args:Array[String])
 {
  val string = "hej"
  string match {
    case e:String => println(string)
    case PersonClass => println(string)
  }
 }
}

When I am doing like this I get error:

pattern type is incompatible with expected type; found : object PersonClass required: java.lang.String case PersonClass => println(string)

And if I then change the second line in the pattern matching to the following:

case e:PersonClass => println(string)

I then get the error:

error: scrutinee is incompatible with pattern type; found : PersonClass required: java.lang.String case e:PersonClass => println(string)

However if I change the string definition to the following it compiles fine in both cases.

val string:AnyRef = "hej"

© Stack Overflow or respective owner

Related posts about scala

Related posts about case-class