Scala Language Named Parameters


When invoking a def, parameters may be assigned explicitly by name. Doing so means they needn't be correctly ordered. For example, define printUs() as:

// print out the three arguments in order.
def printUs(one: String, two: String, three: String) = 
   println(s"$one, $two, $three")

Now it can be called in these ways (amongst others):

printUs("one", "two", "three") 
printUs(one="one", two="two", three="three")
printUs("one", two="two", three="three")
printUs(three="three", one="one", two="two") 

This results in one, two, three being printed in all cases.

If not all arguments are named, the first arguments are matched by order. No positional (non-named) argument may follow a named one:

printUs("one", two="two", three="three") // prints 'one, two, three'
printUs(two="two", three="three", "one") // fails to compile: 'positional after named argument'