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

前端 未结 3 1747
逝去的感伤
逝去的感伤 2020-11-27 12:57

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 o

3条回答
  •  难免孤独
    2020-11-27 13:26

    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.

提交回复
热议问题