I am having trouble understanding why I cannot reference the scala enum type.
The problem is that sometimes I can reference enums:
enum(UserStatus)
and sometimes he complains that he could not find the listing
not found: type UserStatus
Why sometimes I can not refer to the Enumeration class?
The generated source for the enumeration looks fine, and it works fine with the other enumeration that I have, which lives in the same place, the same use ...
Any suggestions?
More details
Generated source for listing:
public final class models.UserStatus extends java.lang.Object{
public static final scala.Enumeration$Value Busy();
public static final scala.Enumeration$Value Free();
public static final scala.Enumeration$ValueSet$ ValueSet();
public static final scala.Enumeration$Value withName(java.lang.String);
public static final scala.Enumeration$Value apply(int);
public static final int maxId();
public static final scala.Enumeration$ValueSet values();
public static final java.lang.String toString();
}
I am trying to implement enum mapper for play framework 2.0
def enumFormat[E <: Enumeration](enum: E): Formatter[E#Value] = new Formatter[E#Value] {
def bind(key: String, data: Map[String, String]) = {
Formats.stringFormat.bind(key, data).right.flatMap { s =>
scala.util.control.Exception.allCatch[E#Value]
.either(enum.withName(s))
.left.map(e => Seq(FormError(key, "error.enum", Nil)))
}
}
def unbind(key: String, value: E#Value) = Map(key -> value.toString)
}
And this method that calls mapper
def enum[E <: Enumeration](enum: E): Mapping[E#Value] = of(enumFormat(enum))
This means that the converter will automatically convert between enumerations when using form binding
Pseudocode of use.
package models {
object UserStatus extends Enumeration {
val Free = Value("free")
val Busy = Value("busy")
}
case class User(
status: UserStatus.Value = UserStatus.Free
)
}
package controllers {
imports models._
val userForm = Form(
mapping(
"status" -> enum(UserStatus)
)(User.apply)(User.unapply)
)
}