How to do pattern matching enumeration?

2,533 views
Skip to first unread message

Léonard Schneider

unread,
Jun 21, 2012, 11:06:20 AM6/21/12
to scala...@googlegroups.com
Hi, 

I'd like to do pattern pattern matching on an enumeration. Naive implementation fails, as shown below:

scala> object Day extends Enumeration { type Day = Value; val monday = Value("Monday"); val tuesday = Value("Tuesday") }
defined module Day
scala> import Day._
import Day._
scala> val d = monday
d: Day.Value = Monday
scala> d match { case tuesday => "Oops"; case monday => "Here I am" }
<console>:13: warning: unreachable code
              d match { case tuesday => "Oops"; case monday => "Here I am" }
                                                               ^
res0: String = Oops

Any idea how to get this in a straightforward way?

Best regards,


Leo


Andreas Flierl

unread,
Jun 21, 2012, 11:12:04 AM6/21/12
to scala...@googlegroups.com
Hi,

lower case identifiers in a match will introduce a new name, e.g.

3 match { case x => println(x) }

will print 3.

If you want to match a constant, you can use backticks:

d match { case `tuesday` => "Oops"; case `monday` => "Here I am" }

Alternatively, you can use upper case constant names (val Monday =... ;
val Tuesday = ...).

Kind regards
Andreas

Léonard Schneider <leonard....@gmail.com> wrote:
> Hi,
>
> I'd like to do pattern pattern matching on an enumeration. Naive
> implementation fails, as shown below:
>
> scala> object Day extends Enumeration { type Day = Value; val monday
> =
> Value("Monday"); val tuesday = Value("Tuesday") }
> defined module Day
> scala> import Day._
> import Day._
> scala> val d = monday
> d: Day.Value = Monday
> scala> d match { case tuesday => "Oops"; case monday => "Here I am" }

Erik Osheim

unread,
Jun 21, 2012, 11:23:12 AM6/21/12
to Léonard Schneider, scala...@googlegroups.com
On Thu, Jun 21, 2012 at 08:06:20AM -0700, L�onard Schneider wrote:
> > scala> d match { case tuesday => "Oops"; case monday => "Here I am" }

Hi,

In this case I think you'll need to enclose your types in backticks
(``) so that Scala doesn't bind the value to the name.

Normally x match { case y => ... } just binds the value to the name
"y". If you want to instead say "this case means x matches y" you'll
need to do:

x match { case `y` => ... }

Hope this helps!

-- Erik

Léonard Schneider

unread,
Jun 21, 2012, 11:47:23 AM6/21/12
to scala...@googlegroups.com, Léonard Schneider
Thanks all. I re-read Programming in scala Pattern Matching section, where it tells what you told. One more thing learned (back quotes in pattern matching). It works great on my use case ;)

BR

Leo

On Thursday, June 21, 2012 5:23:12 PM UTC+2, Erik Osheim wrote:

Naftoli Gugenheim

unread,
Jun 21, 2012, 6:36:21 PM6/21/12
to Léonard Schneider, scala...@googlegroups.com
I think it's more accepted style to use uppercase for enums and the like.

Daniel Sobral

unread,
Jun 21, 2012, 10:49:55 PM6/21/12
to Naftoli Gugenheim, scala-user, Léonard Schneider

It's more useful at any rate. An identifier starting with an uppercase can be used in pattern matching.

Reply all
Reply to author
Forward
0 new messages