Skip to content
Tags

Scala constructors

15 November 2010

Overview of “getting my hands dirty with scala”

Since a while now, I have been trying to get into scala, because I think its a very interesting language with promising concepts. But as usual the scala books just kept lying around, waiting for a better time to come by. But waiting on better times will never result in anything so I decided to just start doing it. In this blog I will post the things that I ran into, hopefully for others to learn from, or otherwise, just for me to read and laugh about again.

To get my hands dirty with scala, I want to implement a code generation tool for java. In my daily work job I have to do a lot of repititive work, so I created a small project to generate some of the fields and methods and testcases automatically. This is done using freemarker templates and java code. Since this project has no dependencies on a spring context or databases it looks like a natural fit to learn some more of scala.

Today I was reading in “Programming in Scala” from Martin Odersky, Lex Spoon and Bill Venners, when I read in chapter 6 that if you place code inside a class (and not within a method) these lines will automatically be included in the constructor. That seemed like a nice feature to me, to check if a fieldDefinition that gets constructed has a classDefinition owner that is not null. So I tried the following:


class ScalaFieldDefinition(classDefinition: ScalaClassDefinition) {
	val templateName: String = "default/fieldDefinition.ftl"
	var defaultInfo: ScalaDefaultInfo = new ScalaDefaultInfo()
	val fieldType: String = defaultInfo.fieldType
	println("in class")
}

After creating a test for this, I saw that indeed the “in class” was printed. So next I wanted to check if the given classDefinition was not null.

Creating the testcase (using scalatest from Bill Venners) resulted in the following


@Test
def shouldThrowIllegalArgumentException {
	intercept[IllegalArgumentException] {
		new ScalaFieldDefinition(null)
	}
}

and I updated the scalaFieldDefinition to:


class ScalaFieldDefinition(classDefinition: ScalaClassDefinition) {
	require(classDefinition != null)

	val templateName: String = "default/fieldDefinition.ftl"
	var defaultInfo: ScalaDefaultInfo = new ScalaDefaultInfo()
	val fieldType: String = defaultInfo.fieldType

	println("in class")
}

After running the testcase, I was greeted with a red bar, indicating no error had been thrown. That was not what I expected, so after a lot of deletions and trials I found out that I should not mix the declarations with the content. So when changed to :


class ScalaFieldDefinition(classDefinition: ScalaClassDefinition) {
	val templateName: String = "default/fieldDefinition.ftl"
	var defaultInfo: ScalaDefaultInfo = new ScalaDefaultInfo()
	val fieldType: String = defaultInfo.fieldType
	require(classDefinition != null)
	println("in class")
}

And that did the trick. Now the check on the required classDefinition did throw an exception.

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: