0
我正在寻找Scala中的enum,它提供了一个依赖于运行时的选项描述。运行时枚举scala中的描述
例如,Answer enum,它允许用户在指定某个消息时指定yes/no和other。
object Answer extends Enumeration {
type Answer = Value
val Yes = Value("yes")
val No = Value("no")
val Other = ???
def apply(id: Int, msg: String = null) = {
id match {
case 0 => Yes
case 1 => No
case _ => Other(msg) ???
}
}
}
用法,因为它遵循:
> Answer(0)
Yes
> Answer(1)
No
> Answer(2, "hey")
hey
> Answer(2, "hello")
hello
这可能吗?还是我应该实施一些层次的案例类?