Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no type arguments for views? #37

Open
benmccann opened this issue Jul 31, 2014 · 14 comments
Open

no type arguments for views? #37

benmccann opened this issue Jul 31, 2014 · 14 comments

Comments

@benmccann
Copy link
Contributor

@refried originally filed as playframework/playframework#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)
  }
}
@jaliss
Copy link

jaliss commented Jan 24, 2015

+1

@mhzajac
Copy link

mhzajac commented Mar 14, 2015

+1, indeed.

@ghost
Copy link

ghost commented Mar 27, 2015

+1

4 similar comments
@dante4d
Copy link

dante4d commented Mar 28, 2015

+1

@1tchy
Copy link

1tchy commented Apr 13, 2015

+1

@motns
Copy link

motns commented Jun 11, 2015

+1

@joepjoosten
Copy link

+1

@julienrf
Copy link
Contributor

julienrf commented Apr 6, 2016

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]}"

@aryairani
Copy link

Aha, thanks @julienrf, that’s good to know.

@intracer
Copy link

intracer commented Aug 3, 2016

big +1

@perryrosenberg
Copy link

+1

1 similar comment
@aryairani
Copy link

+1

@aryairani
Copy link

aryairani commented Aug 2, 2017

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>

@mkurz
Copy link
Member

mkurz commented Nov 10, 2018

Related #153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests