Image Image Image Image Image
Scroll to Top

To Top

code

16

Jun
2011

No Comments

In code
Uncategorized

By admin

Scala pattern matching and variables

On 16, Jun 2011 | No Comments | In code, Uncategorized | By admin

Today I was debugging some scala code for more than one hour. Turns out that if you use a variable in the pattern you are matching you need to put it in back ticks:

val thing = 3
4 match {
    case thing => println("bingo")
}

This code will match because thing will just became a new variable that is a container for whatever values are being matched.

val thing = 3
4 match {
    case `thing` => println("bingo")
    case _ => println("no match")
}

This code on the other hand will not match, because the variable thing is used, whose content is 3.

Tags | ,

Submit a Comment