Function Calls
Now, we'll look at how function calls are implemented in Scala.
Syntax Goodies
Scala provides flexible syntax and it is worth dedicating a few minutes to this concept.
Named Parameters
The following is
a function,
f(a:Int, b:Int)
. We can call this
function using the named parameter syntax:
f(a = 5, b=10)
. If we swap the parameters but leave the correct names, the method will still be correct.
It is possible to combine positional and named function calls—the first few arguments can be positional.
For example:
def f(x:Int, y:Int) = x*2 + y f(x=1,y=2) // 4 f(y=1,x=2) // 5
Default Parameters
When specifying a function, we can set default parameters. Then, later, when we call this function, we can omit parameters and the compiler will substitute defaults:
def f(x:Int, y:Int=2) = x*2 + y f(1) // 4
It is possible to create a comfortable API with the help of the combination of named and default arguments. For example, for case classes with N components, the compiler generates a copy...