Function Values with Multiple Parameters
In the previous example, the function values received one parameter. Function values can receive zero or more parameters. Let’s take a look at a few examples to learn how to define function values with different numbers of parameters.
In its simplest form, a function value may not even receive any parameter, but may return a result. Such a function value is like a factory—it constructs and returns an object. Let’s take a look at an example of defining and using a zero parameter function value:
| def printValue(generator: () => Int) = { |
| println(s"Generated value is ${generator()}") |
| } |
| |
| printValue(() => 42) |
For the method printValue function, we’ve defined the parameter generator as a function ...
Get Pragmatic Scala now with O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.