Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Scala's apply() method magic work?

In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For example:

class Foo(x: Int) {     def apply(y: Int) = {         x*x + y*y     } }  val f = new Foo(3) f(4)   // returns 25 

So basically, object(args) is just syntactic sugar for object.apply(args).

How does Scala do this conversion?

Is there a globally defined implicit conversion going on here, similar to the implicit type conversions in the Predef object (but different in kind)? Or is it some deeper magic? I ask because it seems like Scala strongly favors consistent application of a smaller set of rules, rather than many rules with many exceptions. This initially seems like an exception to me.

like image 496
Jeff Avatar asked Aug 03 '09 18:08

Jeff


People also ask

How does apply work in Scala?

Scala List apply() method with example. The apply() method is utilized to select an element in the list by its index. Return Type: It returns an element from the given list by its index which is present in the apply method as argument.

What is the Apply method?

The apply() method is an important method of the function prototype and is used to call other functions with a provided this keyword value and arguments provided in the form of array or an array like object. Array-like objects may refer to NodeList or the arguments object inside a function.

What does => mean 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 is companion object in Scala?

A companion object in Scala is an object that's declared in the same file as a class , and has the same name as the class. For instance, when the following code is saved in a file named Pizza.scala, the Pizza object is considered to be a companion object to the Pizza class: class Pizza { } object Pizza { }


2 Answers

I don't think there's anything deeper going on than what you have originally said: it's just syntactic sugar whereby the compiler converts f(a) into f.apply(a) as a special syntax case.

This might seem like a specific rule, but only a few of these (for example, with update) allows for DSL-like constructs and libraries.

like image 61
oxbow_lakes Avatar answered Sep 22 '22 08:09

oxbow_lakes


It is actually the other way around, an object or class with an apply method is the normal case and a function is way to construct implicitly an object of the same name with an apply method. Actually every function you define is an subobject of the Functionn trait (n is the number of arguments).

Refer to section 6.6:Function Applications of the Scala Language Specification for more information of the topic.

like image 21
sebasgo Avatar answered Sep 22 '22 08:09

sebasgo