java - Defining and referencing a generic type bound in Play template signature -
i have number of sorted maps, keyed time , value of some type. illustration, consider have 3 maps (in java):
sortedmap<offsetdatetime, foo> foo; sortedmap<offsetdatetime, boo> bar; sortedmap<offsetdatetime, baz> baz;
i wish write generic play template accepts map, , renderer function, , outputs each pair.
within template, can define local function next signature:
@rendertrace[t <: any](trace: immutablesortedmap[offsetdatetime, option[t]], renderer: (t) => html) = {
however, utilize function in multiple templates, not want define locally. rather, had hoped define own template (in rendertrace.scala.html
).
unfortunately, don't seem able able specify type construction [t <: any]
in template signature.
how can define re-usable, generically typed function?
to flesh out @ashalynd's suggestion, template compiler doesn't appear smart plenty handle type parameters. can utilize underscore type parameter (only when don't care type is), isn't 1 of cases.
a play (now called twirl) template function produces result of type play.twirl.api.html
(or play.api.templates.html
play 2.2). templates compiler needs worked around, define helper bundle containing function:
package viewhelpers import play.twirl.api.html // play.api.templates.html 2.2 object viewextension { def rendertrace[t <: any](trace: immutablesortedmap[offsetdatetime, option[t]], renderer: (t) => html): html = ... }
the implementation of rendertrace
might not nice in view template, @ to the lowest degree can work now.
then in view:
@(someparams: ....) @import viewhelpers.viewextension @{viewextension.rendertrace(...)}
java scala templates generics playframework-2.2
No comments:
Post a Comment