79

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Jeff
  • 12,441
  • 15
  • 46
  • 58

3 Answers3

67

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
oxbow_lakes
  • 129,207
  • 53
  • 306
  • 443
  • 4
    I'd add "when `f` is an object (including functions)". When `f` is a method, there is obviously no such translation. – Alexey Romanov Aug 03 '09 at 21:07
  • 2
    @AlexeyRomanov: Well, if `f` is a method that is defined without a parameter list, then `f(a)` is indeed interpreted as something like `val _tmp = f; _tmp.apply(a)`. But that's really getting firmly into the splitting-of-hairs territory. – Jörg W Mittag Dec 05 '14 at 17:37
20

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.

Mario Galic
  • 41,266
  • 6
  • 39
  • 76
sebasgo
  • 3,775
  • 20
  • 28
8

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

Yes. And this rule belongs to this smaller set.

Alexey Romanov
  • 154,018
  • 31
  • 276
  • 433