1
votes

I have the following play2 code:

  implicit def contextToRequest(rc: RequestContext[_]) = rc.request
  implicit def contextToFlash(rc: RequestContext[_]) = rc.request.flash
  implicit def contextToSession(rc: RequestContext[_]) = rc.request.session

  def login: Action[AnyContent] = PublicAction { implicit rc =>
    Ok(html.login(loginForm))
  }

RequestContext is my own custom class wrapper of the Play2 Request.

My scala view starts with:

@(form: Form[(String,String,Boolean)])(implicit flash: Flash, session: Session,rc: utils.RequestContext[_])

But the compiler says:

could not find implicit value for parameter flash: play.api.mvc.Flash

Can someone explain to me why it doesn't work? Perhaps we can't use implicit conversions for implicit parameters?

Then can someone explain to me why there is already a working implicit conversion from the Play2 request to Session/Flash and when I do it it doesn't work?

2

2 Answers

1
votes

When you end up having multiple implicit parameters, some of which are members properties of the others, there is something wrong in your design.

class B
class C

class A {
  val b:B = _
  val c:C = _ 

}

def myMethod(someData:String)(implicit a:A, b:B, c:C) {

}

In fact, what you want to have inside your method are the B and the C members of that A. However, with such a signature one can provide a given A and a B and a C which have nothing to do with that A. The right signature would be the following

  def myMethod(someData:String)(implicit a:A) {
    // trick to import members variable
    import a._
    println(b)
    println(c)
  }
0
votes

Hmmm it seems it works fine with:

implicit def contextToRequest(implicit rc: RequestContext[_]) = rc.request

And i don't need the 2 other methods since there would be an ambiguity because Play2 already provides implicit convertion of Request to Flash/Session