Flexible Parameters and Arguments

Defining parameters and passing arguments are among the most common tasks we perform when programming in any language. Scala offers some nice facilities and conveniences to define variable numbers of arguments, declare default values for parameters, and define named arguments.

Passing Variable Arguments (Varargs)

Methods like println take a variable number of parameters. You can pass zero, one, or more arguments to such methods. In Scala you can create your own functions that can take a variable number of parameters.

We can design methods to take a variable number of arguments—varargs. However, if we have more than one parameter, only the trailing parameters can take a variable number of arguments. To indicate ...

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.