开发者

Scala enumeration to int

Is there a way to identify that a given object is an Enumerati开发者_运维知识库on value?

x.isInstanceOf[Enumeration.$Value] doesn't seem to work, and from what I understand of path-dependent types, shouldn't.

I'd like to write a function that given any Enumeration value, returns its id. Alternatively, an implicit that converts the Enumeration to Int would also be great.


x.isInstanceOf[Enumeration#Value] works for me. From the specification, Section 3.2.2 "Type Projection".


Class scala.Enumeration.Value has a member field 'id' which is the internal integer value. If not specifically specified, it starts from 0.

scala> object Weekday extends Enumeration {           
     | val Mon, Tue, Wedn, Thur, Fri, Sat, Sun = Value
     | }
defined module Weekday

scala> Weekday.Mon.id
res5: Int = 0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜