Scala Repeated Parameters

Little break in Object Oriented Programming and Functional Programming features.

Today is about a little “nice to know”.

You probably understood that this allow you to take an array as input without having to actually create an array when calling the function.

It can be very useful with nested case class with list of list. It makes the code more readable.


case class Foo(f: Foo*)

Foo(
  Foo(
    Foo(),
    Foo()
  ),
  Foo()
)
Imagine this code with having to create List(Foo()) each time, it would make the code really hard to digest.

But, like everything in life, there is a drawback… If you already have an array, you need to use the weird :_*. It is a minor issue, with experience, you are going to know those few characters by heart. It basically turns a list of item into a series of arguments.

Reveal more information and clues
Load Exercise