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