O'Reilly logo

Scala for Java Developers: A Practical Primer by Toby Weston

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required