Friday, April 22, 2011 10:21 PM | rahel luethy | 2 comment(s)

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:

MigrosEasterEgg(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 = MigrosEasterEgg(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!

4:14 PM | Blogger kyb said...

You've inspired me to put my raytracer on github. It's https://github.com/kybernetikos/ScalaTrace/wiki/ScalaTrace if you're interested.

4:31 PM | Blogger rahel luethy said...

Great -- I will look into it asap, thanks for sharing it!