Can't Tell
Can't Tell

Reputation: 13416

overloaded method value async with alternatives:

When I have the following code everything compiles fine.

 def create(name: String, age: Int) = Action.async {
    val json = Json.obj(
      "name" -> name,
      "age" -> age,
      "created" -> new java.util.Date().getTime())

    collection.insert(json).map(lastError =>
      Ok("Mongo LastError: %s".format(lastError)))
  }

But when extract the content to another method as follows

 def create(name: String, age: Int) = Action.async {
   createPerson(name,age)
 }

  def createPerson(name: String, age: Int) = Action.async {
    val json = Json.obj(
      "name" -> name,
      "age" -> age,
      "created" -> new java.util.Date().getTime())

    collection.insert(json).map(lastError =>
      Ok("Mongo LastError: %s".format(lastError)))
  }

it gives the error

[error] /media/pubudu/NTFSPartition/projects/catnet/data-collector-backend/app/controllers/Application.scala:50: overloaded method value async with alternatives:
[error]   [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and>
[error]   (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and>
[error]   (block: => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[error]  cannot be applied to (play.api.mvc.Action[play.api.mvc.AnyContent])
[error]   def create(name: String, age: Int) = Action.async {

Isn't the two code sections doing the same thing? If not, how can I extract the content of the method without getting the error? What I want to do is to call the extracted method from two different places.

Upvotes: 1

Views: 3143

Answers (1)

curious
curious

Reputation: 2928

The create method expects the return type to be Future[Result], but the calling of createPerson returns Action[AnyContent]. You can correct it by making following changes:

def create(name: String, age: Int) = Action.async {
   createPerson(name,age).map(lastError =>
      Ok("Mongo LastError: %s".format(lastError)))
 }

  def createPerson(name: String, age: Int):Future[String] = {
    val json = Json.obj(
      "name" -> name,
      "age" -> age,
      "created" -> new java.util.Date().getTime())

    collection.insert(json)
  }

Upvotes: 4

Related Questions