2

I'm trying how to figure out and work with maps with enums as keys in Scala. Looking at this question, I can instantiate maps, but when I try to update the map in place, I get a type mismatch error. What is going on here?

object MyEnums extends Enumeration {
  type MyEnum = Value
  val BOB, TED, JEN = Value
}

var mymap = scala.collection.mutable.Map[MyEnums.Value, Long]()
mymap += (MyEnums.JEN -> 100L)

throws:

<console>:38: error: type mismatch;
found   : (MyEnums.Value, Long)
required: (MyEnums.Value, Long)
          mymap += (MyEnums.JEN -> 100L)

If I do the same thing, but use e.g. strings as the key type, this works as expected.

EDIT: These issues occur when using scala in spark-shell, not the normal scala repl.

Community
  • 1
  • 1
bill_e
  • 930
  • 2
  • 12
  • 24
  • 1
    This works fine for me in a REPL. What scala version? An is there anything else in scope that you haven't shown us? – Chris Martin May 03 '16 at 21:49
  • Huh weird, I was doing this using the spark shell (spark version 1.6.0, scala version 2.10.5). When I tried this in regular scala repl, (scala version 2.11.7) it works as expected. So is this just a bug that has been fixed? – bill_e May 03 '16 at 21:56
  • Please consider removing or answering the question if it works correctly – pagoda_5b May 03 '16 at 22:45
  • 1
    Reproducible in spark-shell 1.6.0 scala 2.11.7. Probably it has something to do with custom classloader used in spark shell. For example, in [ammonium](https://github.com/alexarchambault/ammonium), a custom scala REPL based on ammonite with spark support, OP's code works fine. I suggest to edit the question to make it clear that error is related to spark-shell. – Aivean May 04 '16 at 00:33

0 Answers0