简体   繁体   English

在Scala中创建一个使用枚举作为键的Map的语法是什么?

[英]What is the syntax for creating a Map in Scala that uses an enum as a key?

please see the below code. 请参见下面的代码。 This line is marked as incorrect by Eclipse: Eclipse将这一行标记为不正确:

var map = Map[MyEnum,Point]()

I am trying to do the scala equivalent of Java: 我正在尝试做等同于Java的scala:

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 编辑:必须进行编辑,因为我在编辑粘贴的代码时搞砸了一些东西,但是它现在应该有效

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. 编辑:对此有一点解释,在枚举中Value是类型和方法的名称。 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 . type MyEnum = Value基本上只是声明Value类型的别名,下一行val UL, U... = Value调用生成枚举的方法,每个枚举的类型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 . 您也可以使用MyEnum.MyEnum ,但是首先声明类型别名的原因是可以将其导入到范围中,并能够像MyEnum一样引用它。

你是不是把MyEnums在地图上,但MyEnum

var map = Map[MyEnum.Value, Point]()
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 . 您可能还想看看Scala中的Case类和Enumerations

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. 因此,此处日记是日记的列表,日记是列表中的特定日记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM