scala 理解Scala枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11067396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding scala enumerations
提问by Karel Bílek
I have to say I don't understand Scala enumeration classes. I can copy-paste the example from documentation, but I have no idea what is going on.
我不得不说我不了解 Scala 枚举类。我可以从文档中复制粘贴示例,但我不知道发生了什么。
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._
- What means
type WeekDay = Valueand why do I have to write that? - Why is
val Mon = Value? What does that even mean? - Why do I have to import the
WeekDayobject? And, - when I write
val day = WeekDay.Mon, why is it typeWeekDay.Value, not typeWeekDay?
- 什么意思
type WeekDay = Value,为什么我必须写那个? - 为什么是
val Mon = Value?那有什么意思? - 为什么我必须导入
WeekDay对象?和, - 当我写作时
val day = WeekDay.Mon,为什么是类型WeekDay.Value,而不是类型WeekDay?
回答by 0__
the Enumerationtrait has a type member Valuerepresenting the individual elements of the enumeration (it's actually an inner class, but the difference doesn't matter here).
该Enumeration特性有一个类型成员Value代表枚举(它实际上是一个内部类,但差异并不重要,在这里)的单个元素。
Thus object WeekDayinherits that type member. The line type WeekDay = Valueis just a type alias. It is useful, because after you import it elsewhere with import WeekDay._, you can use that type, e.g.:
因此object WeekDay继承该类型成员。该行type WeekDay = Value只是一个类型别名。它很有用,因为在使用 将其导入到其他地方后import WeekDay._,您可以使用该类型,例如:
def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
Instead, a minimal version would just be:
相反,最小版本将是:
object WeekDay extends Enumeration {
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
and you do not have toimport the contents of object WeekDay, but then you would need to use type WeekDay.Valueand to qualify individual members. So the example would become
你不必须输入的内容object WeekDay,但你需要使用类型WeekDay.Value和资格个人会员。所以这个例子会变成
def isWorkingDay(d: WeekDay.Value) = ! (d == WeekDay.Sat || d == WeekDay.Sun)
The second question is about the meaning of val Mon, ... = Value. This is indeed very confusing if you don't look into the implementation of Enumeration. This is not the assignment of a type! It is instead calling a protected method of the same name, Value, which returns a concrete instance of type Value.
第二个问题是关于 的含义val Mon, ... = Value。如果您不研究Enumeration. 这不是类型的赋值!这是不是调用一个受保护的同名方法,Value,它返回类型的具体实例Value。
It so happens that you can write val a, b, c = fooin Scala, and for each value a, b, and cthe method foowill be called again and again. Enumerationuses this trick to increment an internal counter so that each value is individual.
碰巧您可以用val a, b, c = fooScala编写,并且对于每个值a, b, 和c该方法foo将被一次又一次地调用。Enumeration使用这个技巧来增加一个内部计数器,以便每个值都是独立的。
If you open the Scala API docs for Enumerationand click on Visibility: All, you will see that method appearing.
如果您打开 Scala API 文档Enumeration并单击Visibility: All,您将看到该方法出现。

