twirl
twirl copied to clipboard
no type arguments for views?
@refried originally filed as https://github.com/playframework/playframework/issues/2194. migrating it here
views/foo1.scala.txt (won't compile)
@[L<:shapeless.Nat:shapeless.ops.nat.ToInt](bar: Bar[L])
@bar has size @shapeless.Nat.toInt[L]
views/foo2.scala.txt (will compile, but silly!)
@(bar: Bar[_<:shapeless.Nat], size: Int)
@bar has size @size
controllers/Application.scala
package controllers
import play.api._
import play.api.mvc._
import shapeless.Nat
import shapeless.ops.nat.ToInt
class Bar[L<:Nat]
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
// won't compile
def foo1 = views.txt.foo1(new Bar[Nat._3])
// needlessly complicated
def foo2 = {
def barSize[L<:Nat:ToInt](bar: Bar[L]): Int = Nat.toInt[L]
val bar = new Bar[Nat._3]
val size = barSize(bar)
views.txt.foo2(bar, size)
}
}
+1
+1, indeed.
+1
+1
+1
+1
+1
Hi, FWIW, a workaround is to use the string interpolator:
import play.twirl.api.StringInterpolation
def foo[L <: Nat : ToInt](bar: Bar[L]): Html =
html"$bar has size ${Nat.toInt[L]}"
Aha, thanks @julienrf, that’s good to know.
big +1
+1
+1
I'd like to replace DomainId
with a type argument; otherwise I'm going to have to duplicate this code all over the place.
@(domains: List[(DomainId, String)])(implicit QPE: QueryParamEncoder[Option[DomainId]])
<select name="domain">
<option value="@{QPE.encode(None).value}">--- ALL DOMAINS ---</option>
@for(pair <- domains) {
<option value="@{QPE.encode(Some(pair._1)).value}">@pair._2</option>
}
</select>
Related #153