Scala 2.8 offers the possibility to call methods using "named arguments", which means specifying the arguments based on the parameter names (instead of the parameter positions). Named arguments help to avoid confusing parameters of the same type and improve code readability.
def [1] resize(width: Int [2], height: Int [2]) = { ... } resize(width = 120, height = 42)
Furthermore, Scala 2.8 allows providing default arguments on method parameters. In current versions of Scala, default arguments have to be implemented manually using method overloading. This causes some level of code duplication which can be avoided using default arguments.
def [1] f(elems: List [2][Int], x: Int [2] = 0, cond: Boolean [2] = true [1]) f(List [2](1)) f(Nil, cond = false [1])
The example also shows how named arguments allow to selectively use defaults of a method: the second application to f uses the default for x, but not the one for cond.
One very useful application of named and default arguments are the compiler-generated "copy" methods in case classes, which allow to create modified copies of an instance using a lightweight syntax. The copy method takes the same type and value parameters as the primary constructor of the case class, and every parameter defaults to the corresponding constructor parameter:
case [1] class [1] A[T](a: T, b: Int [2]) { // def copy[T'](a: T' = this.a, b: Int = this.b): A[T'] = new A[T'](a, b) } val [1] a1: A[Int [2]] = A(1, 2) val [1] a2: A[String [2]] = a1.copy(a = "someString")
Download a recent nightly build (linux-mac [3] / windows [4]) to try out named and default arguments yourself! To read more about named and default arguments, have a look at the corresponding SID [5].
Links:
[1] http://www.scala-lang.org/docu/files/ScalaReference.pdf#
[2] http://www.scala-lang.org/docu/files/api/index.html#
[3] http://www.scala-lang.org/archives/downloads/distrib/files/nightly/distributions/scala-2.8.0.r17995-b20090604111106.tgz#
[4] http://www.scala-lang.org/archives/downloads/distrib/files/nightly/distributions/scala-2.8.0.r17995-b20090604111106.zip#
[5] http://www.scala-lang.org/sid/1#