Scala 枚举中的错误 (?) 类型匹配
我有一个用于表示值类型的枚举类。该类的代码非常简单:
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
另外,我怎样才能避免这种奇怪的情况?如何根据值进行类型匹配以区分不同的枚举?
I have an enumeration class for representing types of values. The code of the class is pretty simple:
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
}
}
}
Where I have the mathing enumerations:
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
}
So, why does the following weirdness occur?:
scala> Type fromValue VAlign.bottom
res3: Type.Type = tHAlign
Also, how can I avoid this weirdness? How can I do type matching from a value to distinguish the different enumerations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您面临着删除路径相关类型的问题(另请参见下面的编辑 2)。
让我们首先简化示例:
现在,与您观察到的类似,
t(Enum1.A)
和t(Enum2.C)
都从第一个枚举打印" “
。我最初认为(请参阅下面的编辑)- 这里发生的是,在模式中使用
:
所产生的instanceOf
测试不会使Value
的两个路径相关实例化,因此第一种情况始终匹配。解决这个问题的一种方法是匹配枚举的值而不是这些值的类型:
编辑1实际上我的假设是与 规范 所说的不匹配。根据语言规范(§8.2 类型模式):
如果我理解正确,
instanceOf
或等效的应该区分这两种情况。编辑 2 似乎是此问题的结果。
I believe you're facing a problem of erased path-dependent types (see also Edit 2 below).
Let's simplify the example first:
Now, similarly to what you observed,
t(Enum1.A)
andt(Enum2.C)
both print"from first enum"
.What — I originally thought (see edit below) — is happening here is that the
instanceOf
test that results from using the:
in the pattern doesn't make the difference between the two path-dependent instantiations ofValue
, so the first case always matches.One way to work around this is to match on the values of the enumeration instead of the type of these values:
Edit 1 Actually my hypothesis does not match what the spec says. According to the language specification (§8.2 Type Patterns):
If I understand this correctly, the
instanceOf
or equivalent should distinguish between the two cases.Edit 2 Seems to be a consequence of this issue.