[ACCEPTED]-How does Scala's apply() method magic work?-companion-object

Accepted answer
Score: 67

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

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

Score: 20

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

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

Score: 8

I ask because it seems like Scala strongly 4 favors consistent application of a smaller 3 set of rules, rather than many rules with 2 many exceptions.

Yes. And this rule belongs 1 to this smaller set.

More Related questions