49

I've found myself stuck on a very trivial thing :-]

I've got an enum:

 object Eny extends Enumeration {
      type Eny = Value
      val FOO, BAR, WOOZLE, DOOZLE = Value
    }

In a code I have to convert it conditionally to a number (varianr-number correspondence differs on context). I write:

val en = BAR
val num = en match {
  case FOO => 4
  case BAR => 5
  case WOOZLE => 6
  case DOOZLE => 7
}

And this gives me an "unreachable code" compiler error for every branch but whatever is the first ("case FOO => 4" in this case). What am I doing wrong?

Yogesh D
  • 1,558
  • 14
  • 29
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • My guess is that "en" is "val" and assigned to "BAR". It can not be reassigned later so it's equals to BAR anywhere after such declaration. – Wildcat Sep 03 '10 at 20:02
  • It's a simplified example, in real program "en" is a function's input argument. It indeed can't be reassigned inside, but can differ from call to call. – Ivan Sep 03 '10 at 20:18
  • 2
    In that case, perhaps you can give a less simplified example? As it stands, the answer is that it gives "unreachable code" error because there is unreachable code. – Alexey Romanov Sep 03 '10 at 20:24
  • 1
    I cannot replicate the error you report. I took your code, unchanged, put it inside an object and compiled it. Did you submit it to the REPL? – Randall Schulz Sep 03 '10 at 20:28
  • Fascinating. After I've isolated the sample in a separate simple .scala file, compiled and ran it - it works as expected. But inside my program it does not. Seems I'll need my tambourine... – Ivan Sep 03 '10 at 20:51
  • I've replaced match-case with if-else-if. Does not look pretty but works at least. – Ivan Sep 03 '10 at 21:13
  • 3
    @Ivan - post your *actual* code! You'll get your solution pretty quickly, I suspect! – oxbow_lakes Sep 03 '10 at 21:27

2 Answers2

65

I suspect the code you are actually using is not FOO, but foo, lowercase, which will cause Scala to just assign the value to foo, instead of comparing the value to it.

In other words:

x match {
  case A => // compare x to A, because of the uppercase
  case b => // assign x to b
  case `b` => // compare x to b, because of the backtick
}
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
7

The following code works fine for me: it produces 6

object Eny extends Enumeration {
  type Eny = Value
  val FOO, BAR, WOOZLE, DOOZLE = Value
}

import Eny._

class EnumTest {
    def doit(en: Eny) = {
        val num = en match {
          case FOO => 4
          case BAR => 5
          case WOOZLE => 6
          case DOOZLE => 7
        }       

        num
    }
}

object EnumTest {
    def main(args: Array[String]) = {
        println("" + new EnumTest().doit(WOOZLE))
    }
}

Could you say how this differs from your problem please?

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • In my real code I have to use lowercases. Daniel says this is the problem. I could never suppose this can matter. – Ivan Sep 03 '10 at 23:14
  • @pagoda_5b 2ns link is dead. Did someone salvaged it somewhere ? – Arioch 'The Aug 06 '13 at 07:18
  • @Arioch'The I'm sorry for the missing link. I suppose the topic was considered too generic for the SO format. I have no substitute at the moment. – pagoda_5b Aug 06 '13 at 14:17