Scala method = trait {...} value

I am trying to learn Scala and Play Framework at the same time. Scala looks like it has a lot of really cool ideas, but one of my disappointments is trying to understand all the different syntaxes for methods / functions / lambdas / anonymous functions, etc.

So, I have my main application controller:

object Application extends Controller {
  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }
}

This tells me that I have a singleton Applicationthat has one method index, which returns which type? I expected indexto have a more important definition:

def index(req: Request) : Result = { ... }

Looking at the documentation for the Play Framework, it looks like it Actionis a sign that converts the query to the result, I can hardly understand what this line says:

def index = Action { ... }

Java, , ? ( , "method index = [some interface Action]", , , - , , ;))

+4
2

, , apply. :.

foo(bar)

foo.apply(bar)

, index Action , , , Action.apply.

index , Action.apply (, , Unit).

+3

, , , : , , Scala apply .

, :

def index = Action {
  Ok("Hello World!")
}

... (, , ) :

def index : Action[AnyContent] = Action.apply(
    (req: Request[AnyContent]) => {
        Ok(views.html.index("Hello World!"))
    } : Result
)

: ... = Action {...}. Action {...} " Action {...}".

Action.apply apply(block: => Result): Action[AnyContent], / .

+1

All Articles