9

please see the below code. This line is marked as incorrect by Eclipse:

var map = Map[MyEnum,Point]()

I am trying to do the scala equivalent of Java:

private enum Letters{ A,B,C}
private Map<Letters,Integer> thing= new HashMap<Letters,Integer> ();

And this is the file/context in which it is written.

class Point(var x:Int = 0, var y:Int = 0, var hasBeenSet:Boolean = false){

}

object MyEnum extends Enumeration{
    MyEnum = Value
    val UL,U,UR,L,R,DL,D,DR = Value
}

object MyEnumHolder {
  var map = Map[MyEnum,Point]()
  MyEnum.values.foreach(x => (map + (x -> new Point()) ) 
}

I am trying to initialize an instance of the map with each value of the enum mapped to an empty point (that's what is going on in the for each loop).

EDIT: Had to edit because I messed some things up editing the pasted code, but it should be valid now

jhnclvr
  • 9,137
  • 5
  • 50
  • 55

4 Answers4

14
var map = Map[MyEnum.Value, Point]()

or I prefer

import MyEnum._
var map = Map[MyEnum,Point]()

edit: To give a little explanation of what this means, in the enumeration Value is the name of both a type and a method. type MyEnum = Value is basically just declaring an alias for the Value type, and the next line val UL, U... = Value is calling the method to generate the enums, each of which has type MyEnum.Value. So when declaring the map, you have to refer to this type in order for the key to store enums. You could also use MyEnum.MyEnum, but the reason you declare the type alias in the first place is so you can import it into scope and be able to refer to it just as MyEnum.

Dan Simon
  • 12,891
  • 3
  • 49
  • 55
  • That did the trick! Thanks! I don't understand the meaning of .Value though I think is my problem, will have to look that up. – jhnclvr Jul 11 '12 at 20:20
2

You are not putting MyEnums in the map, but values of MyEnum:

var map = Map[MyEnum.Value, Point]()
Landei
  • 54,104
  • 13
  • 100
  • 195
2
object MyEnum extends Enumeration {
  type MyEnum = Value /* You were missing keyword "type" here */
  val UL,U,UR,L,R,DL,D,DR = Value
}

object MyEnumHolder {
  import MyEnum.MyEnum /* Import this, otherwise type MyEnum can't be found */

  var map = Map[MyEnum, String]()
  MyEnum.values.foreach(x => (map + (x -> x.toString)))
}

You might also want to have a look at Case classes vs Enumerations in Scala.

Community
  • 1
  • 1
Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
1

I got it to work with the following. The syntax is a bit wonky, I'll admit.

object MyEnum extends Enumeration {
    type MyEnum = Value
    val UL, U, UR, L, R, DL, D, DR = Value
}

object Main extends App {
    Map[MyEnum.MyEnum, String]()

}

Although I guess it can help for pluralization

object Diaries extends Enumeration {
    type Diary = Value
    val Steven, Jessica, Bob = Value
}

import Diaries._

val currentDiary = Steven

val interestingDiaries = List[Diary](Steven, Jessica)

So here Diaries is the list of Diary, and Diary is a specific diary in the list.

Laurent Bourgault-Roy
  • 2,774
  • 1
  • 30
  • 36