Above, Below, and Beyond Tech Talk

by Rahel Lüthy

April 22, 2011

Scala Gems #3: Named Parameters

Just a quick one this time (it’s easter holiday and the weather is simply too beautiful to waste time in front of a computer). Anyway, have a look at this constructor call:

MigrosEgg(GREEN, BLUE)

Unless you’re Swiss and just know that I’m referring to the “Extra” variant, wouldn’t it be a lot less ambiguous to write

val aMigrosEgg = MigrosEgg(dotColor = GREEN, bgColor = BLUE)

As of Scala 2.8 you can name your arguments. You can also leave out those which have default values, shuffle their order – or you can simply have another one of those little suckers:

val notAMigrosEgg = aMigrosEgg.copy(dotColor = PINK)

Happy easter everyone!