Created
January 11, 2012 16:00
-
-
Save kmizu/1595340 to your computer and use it in GitHub Desktop.
Get runtime type information of a Enumeration's subclass from a Value's instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> object W extends Enumeration { val A = Value } | |
defined module W | |
scala> val V: Enumeration#Value = W.A | |
V: Enumeration#Value = A | |
scala> val VClass = V.getClass() | |
VClass: java.lang.Class[_ <: Enumeration#Value] = class scala.Enumeration$Val | |
scala> val outerM = VClass.getMethod("scala$Enumeration$Val$$$outer") | |
outerM: java.lang.reflect.Method = public scala.Enumeration scala.Enumeration$Val.scala$Enumeration$Val$$$outer() | |
scala> val WClass = outerM.invoke(V).getClass() | |
WClass: java.lang.Class[_ <: java.lang.Object] = class W$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code. My problem is to create an Enumeration.Value from String (e.g., "A"):
I'm not sure how to create Fruit.Banana from String "Banana" without knowing enclosing Fruit class.