Some language questions (about the "Action" and "Ok" concepts from the Scala Play API)

1,258 views
Skip to first unread message

jsamot

unread,
Mar 26, 2012, 4:36:03 AM3/26/12
to scala-user
I am Scala newbie, and when reading some code examples at
https://github.com/playframework/Play20/wiki/ScalaActions
there are some things I do not understand with this little code
example:

val echo = Action { request =>
Ok("Got request [" + request + "]")
}

Here is the documentation of a Scala Play Action:
http://scala.playframework.org/documentation/api/2.0/scala/index.html#play.api.mvc.Action
trait Action [A] extends (Request[A]) ? Result with Handler
...
def apply (request: Request[A]): Result

I am a bit familiar with the Trait concept (e.g. as examplified at
http://www.scala-lang.org/node/126 ) but still do not understand what
is going on here considering the instantiation.
The keyword "new" is not used here. Does it mean that the apply method
of the trait is invoked to instantiate it ?
What does the "{" mean at the instantiation of an Action, I mean when
using "Action { ..." instead of something like "new Action( ..." i.e.
curly braces instead of parenthesis ?
(is there any link to a page explaining this kind of instantiation ?)
How can you figure out what the type "A" is in 'trait Action [A]' in
the context of the Play framework ?

What is the "Ok" thing ? I mean, is it a method with parameters or
what is it ?
Since the above field ("echo") is executing from a class inheriting
from Controller, I checked this documentation:
http://scala.playframework.org/documentation/api/2.0/scala/index.html#play.api.mvc.Controller
There it seems to be an overloaded constant
(either "val OK : Int" or "val Ok : Status")
but if so, then how can a constant take a parameter ?
I mean the '("Got request [" + request + "]")' seems to me as being a
parameter of a method when looking at the row 'Ok("Got request [" +
request + "]")'

Dave

unread,
Mar 26, 2012, 5:23:01 AM3/26/12
to scala-user
There is a Play mailinglist:
http://groups.google.com/group/play-framework

On 26 mrt, 10:36, jsamot <samo...@gmail.com> wrote:
> I am Scala newbie, and when reading some code examples athttps://github.com/playframework/Play20/wiki/ScalaActions
> there are some things I do not understand with this little code
> example:
>
> val echo = Action { request =>
>   Ok("Got request [" + request + "]")
>
> }
>
> Here is the documentation of a Scala Play Action:http://scala.playframework.org/documentation/api/2.0/scala/index.html...
> trait Action [A] extends (Request[A]) ? Result with Handler
> ...
> def apply (request: Request[A]): Result
>
> I am a bit familiar with the Trait concept (e.g. as examplified athttp://www.scala-lang.org/node/126) but still do not understand what
> is going on here considering the instantiation.
> The keyword "new" is not used here. Does it mean that the apply method
> of the trait is invoked to instantiate it ?
> What does the "{" mean at the instantiation of an Action, I mean when
> using "Action { ..." instead of something like "new Action( ..." i.e.
> curly braces instead of parenthesis ?
> (is there any link to a page explaining this kind of instantiation ?)
> How can you figure out what the type "A" is in 'trait Action [A]' in
> the context of the Play framework ?
>
> What is the "Ok" thing ?  I mean, is it a method with parameters or
> what is it ?
> Since the above field ("echo") is executing from a class inheriting
> from Controller, I checked this documentation:http://scala.playframework.org/documentation/api/2.0/scala/index.html...

jsamot

unread,
Mar 26, 2012, 9:16:43 AM3/26/12
to scala-user
I know there is a Play list, but I am pretty sure that if I would ask
this kind of basic question about the Scala language, then they would
reply something like:
"This question is not specifically about the Play framework. If you
just would learn the Scala language, then you would not need to ask
such a trivial question"

Therefore I asked these newbie questions (with example code from Play
framework) in this "scala-user" group intended for the following
purpose:

http://groups.google.com/group/scala-user/about
> "the following should go to this list: newbie questions."

Josh Suereth

unread,
Mar 26, 2012, 9:38:37 AM3/26/12
to jsamot, scala-user
This is the correct list to ask *scala* questions, which you have.  Not sure I can help much with Play details, but here's the Scala bits you need.

In Scala, any object that has an 'apply' method can be treated as if it were a function.   e.g.

object foo {
  def apply(x: Int) = prinltn(x)
}

foo(1)   // Prints 1

In play, Action + Ok are both top-level objects (singletons if you will).   You want to look at the documentation for the *object* not the trait.  (Click the 'O' icon beside the 'T').

Now, you can pass "expressions" as arguments to a method, the same as anything else.   For example, in OK, I'm pretty user it takes a "by-name" parameter expression:

object Ok {
  def apply[A](k: => A): Result = ...
}


So when you write:
  Ok("Got request [" + request + "]")

the compiler rewrites it as:

  Ok.apply[String]("Got request [" + request + "]")

*technically, it gets rewritten into something a bit more foreign, but hopefully you get the idea.


Now for Action, it probably takes a *function* as its argument.   So you see something like this:


object Action {
  def apply(function: Request => Response): SomeReturnValue = ...
}

In scala, we can invoke Action with a function block:

Action { request =>  ..... }

This is rewritten as:

Action.apply({ request => ... })

You may be wondering what the implicit keyword is doing.  It's a shorthand convenince to make the argument to a function available on the implciit scope within the function.   In other words:

Action apply { implicit request => ... }

is the same as:

Action apply { request =>
  implicit val _some_random_name_ = request
  ...
}


Hope that Helps!

- Josh

Chris Beach

unread,
May 20, 2013, 6:18:55 AM5/20/13
to scala...@googlegroups.com, jsamot
Hi Josh,

Thanks for this explanation. Like the original poster, I was also curious to know what was going on!

Chris
Reply all
Reply to author
Forward
0 new messages