views:

219

answers:

2

I'm in scala writing a serializer that saves an object (or Model) to the database (for app engine), and I need to treat some fields as special cases. For example, if the field is of type Array[Byte], I Save it as a blob. And I need to treat Enumerations as special cases too, but I can't find out how to know if a type is an enumeration.

For example:

object UserType extends Enumeration {
    val Anonym, Registered, Admin, Super = Value
}

var value = UserType.Admin  
value.isInstanceOf[Enumeration] // this returns false

Neither I can do value.isInstanceOf[Enumeration.Value] since Value is private... anyway I think that would return false too.

Any idea?
Thanks!

+2  A: 
value.isInstanceOf[Enumeration$Value]
Walter Chang
+2  A: 

You could figure this out using these methods:

scala> value.getClass              
res102: java.lang.Class[_] = class scala.Enumeration$Val

scala> value.getClass.getSuperclass
res103: java.lang.Class[_ >: ?0] = class scala.Enumeration$Value

scala> value.getClass.getSuperclass.getSuperclass
res104: java.lang.Class[_ >: ?0] = class java.lang.Object
Alex Neth