Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Scala Blocks

Tags:

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.

like image 1000
b1nd Avatar asked Sep 25 '13 14:09

b1nd


People also ask

What is the meaning of => in Scala?

=> 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 .

What does _* mean in Scala?

: _* 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.

What is some () in Scala?

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.

What do you call a function defined in a block in Scala?

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 ( . )


1 Answers

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("...")) }) 
like image 169
om-nom-nom Avatar answered Sep 22 '22 07:09

om-nom-nom