【发布时间】:2011-10-21 20:48:35
【问题描述】:
我有一个用于表示值类型的枚举类。类的代码很简单:
object Type extends Enumeration {
type Type = Value
val tInt, tBoolean, tString, tColor, tFont, tHAlign, tVAlign, tTextStyle, tUnknown = Value;
def fromValue (value:Any) : Type.Type = {
value match {
case a:Int => tInt
case a:Boolean => tBoolean
case a:Color => tColor
case a:Font => tFont
case a:HAlign.HAlign => tHAlign
case a:VAlign.VAlign => tVAlign
case a:TextStyle.TextStyle => tTextStyle
case _ => tUnknown
}
}
}
我有数学枚举的地方:
object VAlign extends Enumeration {
type VAlign = Value
val top, middle, bottom = Value
}
object HAlign extends Enumeration {
type HAlign = Value
val left, center, right = Value
}
object TextStyle extends Enumeration {
type TextStyle = Value
val bold, italic, regular = Value
}
那么,为什么会出现以下奇怪现象?:
scala> Type fromValue VAlign.bottom
res3: Type.Type = tHAlign
另外,我怎样才能避免这种怪异现象?如何从值进行类型匹配以区分不同的枚举?
【问题讨论】:
标签: scala enumeration