scala沒有從語法的角度來支持枚舉,而是通過定義了一個接口Enumeration來支持的
object ExecutorState extends Enumeration{ type ExecutorState = Value val LAUNCHING, LOADING, RUNNING, KILLED, FAILED, LOST, EXITED = Value def isFinished(state:ExecutorState):Boolean = { Seq(KILLED, FAILED, LOST, EXITED).contains(state) } }
上面是spark中的一個例子,使用type來定義一個同名的類型, 一般就是枚舉的類型.
Value的可以傳遞參數,有下面幾種方法聲明
protected final def Value : Enumeration.this.Value = { /* compiled code */ } protected final def Value(i : scala.Int) : Enumeration.this.Value = { /* compiled code */ } protected final def Value(name : scala.Predef.String) : Enumeration.this.Value = { /* compiled code */ } protected final def Value(i : scala.Int, name : scala.Predef.String) : Enumeration.this.Value = { /* compiled code */ }
大致使用就這么多吧.如果有新的后面在編輯吧