After a lot of work in Java, I became interested in Scala. As a training project, I am trying to duplicate a java library that stores and retrieves state objects from a database. To do this, I would simply indicate the state object as follows:
@PersistName("PERSON") case class Person extends Entity {
@Persist var id:Long = -1
@Persist @MaxLength(80) var firstName = ""
@Persist @MaxLength(80) var lastName = ""
@Persist var gender = Gender.Male
@Persist @MaxLength(80) var userName = ""
@Persist @OptionClass(classOf[Date]) var birthDay:Option[Date] = None
}
The code to serialize / un -serialize an instance of Person uses reflection to know the types of fields and works fine for everyone except the gender field. The gender field is an enumeration that is defined as:
object Gender extends Enumeration {
type Gender = Value
val Male,Female,Unknown = Value
}
The problem is that I don’t know how I can use reflection, I also create a new Gender value using only the Person class.
Peter Muys
source
share