Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reference scala Enumeration from other package
    text
    copied!<p>I'm having trouble understanding why I can't reference a scala enum type.</p> <p>The problem is that sometimes I can reference enums:</p> <pre><code>enum(UserStatus) </code></pre> <p>and sometimes it complains about not being able to find the enum</p> <pre><code>not found: type UserStatus </code></pre> <p>How come I sometimes cannot reference the Enumeration class?</p> <p>The generated source for the enum seems to be ok, and it works great with another enum that I have which lives in the same place, same usage...</p> <p>Any Suggestions?</p> <h1>More info</h1> <p>The generated source for the enum is:</p> <pre><code>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(); } </code></pre> <p>I'm trying to implement an enum mapper for play framework 2.0</p> <pre><code>def enumFormat[E &lt;: 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 =&gt; scala.util.control.Exception.allCatch[E#Value] .either(enum.withName(s)) .left.map(e =&gt; Seq(FormError(key, "error.enum", Nil))) } } def unbind(key: String, value: E#Value) = Map(key -&gt; value.toString) } </code></pre> <p>And this method that calls the mapper</p> <pre><code>def enum[E &lt;: Enumeration](enum: E): Mapping[E#Value] = of(enumFormat(enum)) </code></pre> <p>This means that the mapper automatically converts between a enum when using the form binding</p> <p>Pseudo code of usage.</p> <pre><code>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" -&gt; enum(UserStatus) )(User.apply)(User.unapply) ) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload