Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play framework asynchronous results

I changed one function from:

def submit = Action { request =>
  signupForm.bindFromRequest()(request).fold(
    // Form has errors
    errors => BadRequest(html.signup.form(errors)),

    // We got a valid User value, display the summary
    user => {
      // intensive computation involving database
      Ok("okay")
    }
  )
}

to

def submit = Action { request =>
  val result = Akka.future {
    signupForm.bindFromRequest()(request).fold(
      // Form has errors
      errors => BadRequest(html.signup.form(errors)),

      // We got a valid User value, display the summary
      user => {
        // intensive computation involving database
        Ok("okay")
      }
    )
  }
  Async {
    result
  }
}

and I get the compilation error of:

[error]  found   : play.api.mvc.SimpleResult[_ >: java.lang.String with play.api.templates.Html <: java.io.Serializable]
[error]  required: play.api.mvc.SimpleResult[_1(in value result)] where type _1(in value result) >: java.lang.String with play.api.templates.Html <: java.io.Serializable
[error] Note: java.io.Serializable >: _1, but class SimpleResult is invariant in type A.
[error] You may wish to define A as -A instead. (SLS 4.5)
[error]       signupForm.bindFromRequest()(request).fold(
[error]                                                 ^
[error] one error found

The error message seem like it has something to do with variance. Does anyone understand what's going on?

like image 777
platypus Avatar asked Jul 22 '26 16:07

platypus


1 Answers

BadRequest is returning the type SimpleResult[Html]
Ok is returning the type SimpleResult[String]

If you make BadRequest and Ok return the same type then it would work.

Try doing Ok(Html("ok")) - or actually render a page.

like image 86
Ivan Meredith Avatar answered Jul 25 '26 08:07

Ivan Meredith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!