了解scala枚举 [英] Understanding scala enumerations

查看:121
本文介绍了了解scala枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不说我不明白Scala枚举类。我可以从文档中复制粘贴示例,但我不知道发生了什么。

  object WeekDay extends Enumeration {
类型WeekDay =价值
val星期一,星期二,星期三,星期四,星期五,星期六,太阳=价值
}
导入WeekDay._




  • 什么意思键入WeekDay = Value ,为什么我有写这个

  • 为什么 val Mon = Value ?这是甚么意思?

  • 为什么我必须导入 WeekDay
    对象?而且,当我写 val day = WeekDay.Mon ,为什么键入 WeekDay.Value

  • ,不输入 WeekDay

解决方案

枚举 trait有一个类型成员 Value 代表枚举的各个元素(它是实际上是一个内部类,但差异在这里并不重要。)



因此对象WeekDay 继承该类型成员。 类型WeekDay = Value 只是一个类型别名。这是有用的,因为在其他地方导入 import WeekDay ._ 之后,您可以使用该类型,例如:

  def isWorkingDay(d:WeekDay)=! (d == Sat || d == Sun)

相反,最小版本只是:

 对象WeekDay extends枚举{
val Mon,Tue,Wed,Thu,Fri,Sat,Sun = Value
}

你不要导入内容对象WeekDay ,但是您需要使用类型 WeekDay.Value 并限定个别成员。所以这个例子将成为

  def isWorkingDay(d:WeekDay.Value)=! (d == WeekDay.Sat || d == WeekDay.Sun)






第二个问题是关于 val Mon,... = Value 的含义。如果您不考虑枚举的实现,这确实非常混乱。这不是一个类型的赋值!而是调用相同名称的保护方法 Value ,该方法返回一个类型为 Value



发生这样的事情,你可以写$ val a,b,c = foo Scala,对于每个值 a b c 方法 foo 将被重复调用。 枚举使用这个技巧来增加内部计数器,以使每个值都是个别的。



如果您打开Scala API文件枚举,然后点击可见性:全部,您将看到该方法出现。


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.

object WeekDay extends Enumeration {
  type WeekDay = Value
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._

  • What means type WeekDay = Value and why do I have to write that?
  • Why is val Mon = Value? What does that even mean?
  • Why do I have to import the WeekDay object? And,
  • when I write val day = WeekDay.Mon, why is it type WeekDay.Value, not type WeekDay?

解决方案

the Enumeration trait has a type member Value representing the individual elements of the enumeration (it's actually an inner class, but the difference doesn't matter here).

Thus object WeekDay inherits that type member. The line type WeekDay = Value is just a type alias. It is useful, because after you import it elsewhere with import WeekDay._, you can use that type, e.g.:

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 to import the contents of object WeekDay, but then you would need to use type WeekDay.Value and to qualify individual members. So the example would become

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.

It so happens that you can write val a, b, c = foo in Scala, and for each value a, b, and c the method foo will be called again and again. Enumeration uses this trick to increment an internal counter so that each value is individual.

If you open the Scala API docs for Enumeration and click on Visibility: All, you will see that method appearing.

这篇关于了解scala枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆