KingOfCoders
KingOfCoders

Reputation: 2282

How to use Status in own code in Play

Playversion is 2.6.12.

I want to use Results.Status in my own code like

   // in my code somewhere
   def doSomething(status:Results.Status) { ....} 

   // in controller
   doSomething(Ok)   <---- results in error

But I the Ok inside a controller seems not to be of type Results.Status

[error] found : MyController.this.Status
[error] required: play.api.mvc.Results.Status

Any ideas how to use Status in my own code outside controllers?

The helper code is

import org.scalactic.{Bad, Good, Or}
import play.api.libs.json.Json
import play.api.mvc.Result
import play.api.mvc.Results.Status

object Helper {

  def toResult[T](r:Or[T, Result], s:Status):Result = {
    r match {
      case Good(entity) => s(Json.toJson(entity))
      case Bad(badRequest) => badRequest
    }
  }
}

The controller code definition is extending

class AuthBaseController @Inject()(acc: AuthControllerComponents) extends BaseController with AuthRequestMarkerContext {

where BaseController leads to

trait ControllerHelpers extends Results with HttpProtocol with Status with HeaderNames with ContentTypes with RequestExtractors with Rendering with RequestImplicits

extending Status.

Upvotes: 0

Views: 56

Answers (2)

Philluminati
Philluminati

Reputation: 2789

[error] found : MyController.this.Status
[error] required: play.api.mvc.Results.Status

Status is a specific type that depends on the controller. To have a function that's a certain type, that doesn't care about the parent type you can use the # symbol like this:

def doSomething(status:Results#Status) { ....} 

Upvotes: 0

Zolt&#225;n
Zolt&#225;n

Reputation: 22166

You can always explicitly pass the requested type:

doSomething(play.api.mvc.Results.Ok)

I suppose if you just import play.api.mvc.Results._ in your controller, you will have an import conflict (you can still try it), but just importing play.api.mvc.Results, and then using it like this

doSomething(Results.Ok)

should work.

Upvotes: 1

Related Questions