I'm having difficulty finding specific answers to what I know is something trivial. I would like to understand how blocks work in Scala. I come from a java/ruby background and it seems that scala has an entirely different way of using blocks.
The following code is from the Play! Framework website. I would like to understand what Action is semantically. Is it an object or a function that accepts a block, or perhaps neither.
object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } }
If it is a function, perhaps it's syntactic sugar for the following (in which case how does scala pass around blocks behind the scenes):
def index = Action({ Ok(views.html.index("Your new application is ready.")) })
Or is it some scala syntax I'm unaware of.
Any references to Scala source code would help me understand how this is working behind the scenes.
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
: _* is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence, i.e. varargs.
Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.
A method is a function defined in a class and available from any instance of the class. The standard way to invoke methods in Scala (as in Java and Ruby) is with infix dot notation, where the method name is prefixed by the name of its instance and the dot ( . )
You would be better to think about scala blocks as java anonymous classes (e.g. like Guava Functions) rather than as ruby blocks. Actually speaking, if you decompile scala code you will see more or less the same code (taken from Guava examples):
Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } };
The difference is that scala provides a lot of syntax sugar and allows you to write above code as:
val lengthFunction = { string: String => string.length }
As for a specific Action example:
def index = Action({ Ok(views.html.index("Your new application is ready.")) })
Here Action is likely object with apply method. Another scala sugar: language allows you to write Foo(bar)
and mean Foo.apply(bar)
. Next, you can drop round braces when your call isn't ambiguous, so yes, it is actually a method that got called like:
def index = Action({ Ok(views.html.index("Your new application is ready.")) })
And have something like this as a signature:
object Action { def apply(block: => Result) = ??? }
As already said by @yan, it is scala way to say hey, I'm a function that accept another function that produces Result
Desugared invocation will look like
def index = Action.apply(new AbstractFunction[Result] { def apply() = Ok.apply(views.html.index.apply("...")) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With