Skip to content

Scala Constructors with Scala 2.8

24 November 2010

Last week at the devoxx, I watched a presentation on “What’s new in Scala 2.8” and one thing I really liked in this presentation (and thus in Scala 2.8) was the notion of named and default parameters. What this means is that if you have a number of parameters, you can give them default values, so if they are not filled in when being called, the default values will be set as the parameters. This is something that is already present in a number of languages, but was (and still is) missing in java. Luckily my new preferred language to be has this now as well. When using this correctly, the code can become even more readable, since not all parameters have to be specified.

For instance, given the following definition:

class ScalaClassDefinition(info: ScalaDefaultInfo) {

When constructing a ScalaClassDefinition I had to give a scalaDefaultInfo as parameter before but now I can just fill in the default value, e.g.

class ScalaClassDefinition(info: ScalaDefaultInfo = new ScalaDefaultInfo()) {

And then the construction could be done without the scalaDefaultInfo, e.g.:

val definition = new ScalaClassDefinition()

Of course, I already had something similar set up using another constructor that would call the default constructor with a new for scalaDefaultInfo as in:

class ScalaClassDefinition(info: ScalaDefaultInfo = new ScalaDefaultInfo()) {
    def this() = this (new ScalaDefaultInfo())
}

but using the default parameter felt just a bit cleaner. This can also be applied for parameters in methods of course. However this will not cover your ass when you assign a null value to a named parameter. This will result in assigning null to the parameter itself. For instance, when parsing a piece of xml and looking for some field that will be passed on to the constructor, if the field is not found, the default value will not be set, since you are actually passing in a parameter, e.g.

Given a piece of xml like >info type=”something”/<
And a constructor like:

class GeneralInfo(val type:String = "myType", lookup:String = "lookAtMe")

If looking for an additional field called lookup and if this is not found, use the default value, then you have to assign that default value yourself if you set this on constructor, e.g.:

val type = if (content \ "@type" == NodeSeq.Empty) null else (content \ "@type) text
val lookup = if (content \ "@lookup" == NodeSeq.Empty) null else (content \ "@lookup) text
val generalInfo: GeneralInfo = new GeneralInfo(lookup = lookup, type = type)

This will result in a GeneralInfo object where the lookup field is set to null and not to its default. The default will only be applied, if there is no field present in the constructor for that field.

Using named parameters you can refer to parameters in a method by specifying its name. This is mostly usefull (I think) in a method that takes a lot of paramters that are all of the same signature. E.g. compare this call:

sendEmail("jon.pretty@example.com", List("recipient@example.com"), "Test email", b, Nil, Nil, Nil)

to this call:

sendEmail(from = "jon.pretty@example.com", to = List("recipient@example.com"), subject = "Test email", body = b)

You can read more of this feature on Bill Venner’s own page: http://www.artima.com/scalazine/articles/named_and_default_parameters_in_scala.html

Advertisements

From → scala, Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: