Gregory Cheng Gregory Cheng - 3 years ago 195
Scala Question

Match Statement Case Objects in Scala

I'm currently working on a project in Scala and ran into a small issue.

I'm currently using a match statement to determine the type of a case object. I'm going through this by saying:

abstract class Symbol
case object program extends Symbol
case object stmt_list extends Symbol
case object stmt extends Symbol
case object expr extends Symbol
case object term_tail extends Symbol
case object term extends Symbol
case object factor_tail extends Symbol
case object factor extends Symbol
case object add_op extends Symbol
case object mult_op extends Symbol


def expected_symToIndex(expected_sym: Symbol): Int = expected_sym match {

case program => 0
case stmt_list => 1
case stmt => 2
case expr => 3
case term_tail => 4
case term => 5
case factor_tail => 6
case factor => 7
case add_op => 8
case mult_op => 9

}


In Eclipse, this is giving me a warning saying that


"patterns after a variable pattern cannot match (SLS 8.1.1) If you
intended to match against object program in package , you must
use backticks, like: case program ⇒"


on the first line. Everything else is unreachable code.

After testing my program, this method always returns 0 (because it always executes on the first case object line in the method). I've been searching around for case object match methods, but haven't found a lot of material similar to what's going on here. I'm confused because these Symbols aren't variables within themselves; they are types that I am checking for inside of the match statement.

Further, surrounding "program" and other tested values with backticks aren't working. When I try something like this to test the type:

case a: program => 0


The compiler is telling me that the type "program" cannot be found.

Thanks!

Answer Source

Your program, stmt, expr, etc. are defined as objects not classes and thus they are values not types. This should explain why you cannot use case a: program =>.

Also, match/case in Scala does not work how you described it. If you use a variable inside a case like x match { case a => ... }, then the program does NOT check whether x is equal to a and then proceed to run ... – no it will match ANY value and assign it to the variable a, so you can use a inside the ... to refer to the matched value.

As you described this is not what you want to do. When you do not want to match any value and assign it, but instead match a specific value of a variable, you can mark the variable using backticks (as the error message tried to tell you), like this x match { case `a` => ... }.

Another way to get around the problem is, by marking the objects as immutable, because then it is clear to the compiler that you do not intend to assign to them but only use them. You can do so, by using case object instead of object.

See also https://alvinalexander.com/scala/scala-unreachable-code-due-to-variable-pattern-message

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download