© Toby Weston 2018
Toby WestonScala for Java Developershttps://doi.org/10.1007/978-1-4842-3108-1_14

14. Faking Function Calls

Toby Weston
(1)
London, UK
 
Scala provides mechanisms to make method calls look like regular function calls. It uses special case apply and update methods to allow a kind of shorthand call notation that can reduce the clutter of your code.

The apply Method

The apply method provides a shorthand way of calling a method on a class. So, as we saw, you can use them as factory-style creation methods, where given some class, such as our old friend Customer:
  class Customer(name: String, address: String)
…you can add an apply method to its companion object.
  object Customer {
    def apply(name: String, address: String) = new ...

Get Scala for Java Developers: A Practical Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.