8000 feature request: add Scala 3 context function methods to Action · Issue #13595 · playframework/playframework · GitHub
[go: up one dir, main page]

Skip to content

feature request: add Scala 3 context function methods to Action #13595

@xuwei-k

Description

@xuwei-k

https://github.com/playframework/playframework/blob/561f4921e5da477658b8e6b34bd48292b42ac9fe/core/play/src/main/scala/play/api/mvc/Action.scala#L360C2-L428C92

There are some Action construct methods.

  def apply(block: R[B] => Result): Action[B] =
  def apply(block: => Result): Action[AnyContent] =
  def async(block: => Future[Result]): Action[AnyContent] =
  def async(block: R[B] => Future[Result]): Action[B] =
  def async[A](bodyParser: BodyParser[A])(block: R[A] => Future[Result]): Action[A] =

some common use cases request implicit variable not reference explicit in Action.

class MyController {
  def hello(id: Int): Action[AnyContent] = Action { implicit request =>
    obj.myMethodImplicitRequest(id)
  }
}
def myMethodImplicitRequest(id: Int)(implicit request: Request[?]) =

I think this is proper use case Scala 3 context function.
We can remove boilerplate implicit request => by context function.

https://docs.scala-lang.org/scala3/reference/contextual/context-functions.html

class MyController {
+  def hello(id: Int): Action[AnyContent] = Action.someContextFunctionMethod {
-  def hello(id: Int): Action[AnyContent] = Action { implicit request =>
    obj.myMethodImplicitRequest(id)
  }
}

We should add new name method in src/main/scala-3 instead of apply or async overloads but I can’t think of good method name.

WDYT?

I have added some context function methods in scalikejdbc because there is similar use case

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0