Five random lessons learned about Scala

Since a week I am playing around with Scala to see what all this fuss is about. After Twitter has anounced that they are using Scala (see this presentation) there is a real hype now. So far I must admit I am impressed. This language is really nice. I will certainly have a very detailed look. The book “Programming in Scala” is already on its way from Amazon.

The following lessons learned are just a kind of note to self. These are some random experiences I made when playing around. Your first lessons will probably be completely different.

1. Scala != Groovy
Although this is obvious, when you have prior experience in Groovy it is tempting to fall into your Groovy habits. Both languages look similar at a glance but are very different in detail. For instance I very often write def when defining a variable where it should be val or var. The fact that def is a valid keyword in Scala and is used to define methods is not helpful in this regard. There are so many other subtile differences that I can’t mention them here. Just be aware of that.

2. Use a decompiler
A decompiler can be very useful when learning Scala. Just compile the Scala class and then decompile it. This way you can easily see, what the Scala compiler is creating when using certain language features. I discovered a nice decompiler while doing this. It can be found here. The decompiler was key for solving the next issue.

3. Be careful with field level annotations
It is a great feature of Scala that you can use established Java frameworks and just replace the Java code with Scala. This way you can easily use Spring and the likes. I did that but had sudden problems with the Spring @Autowired annotation. I annotated the field like that:

@Autowired
var pollService: IPollService = _;

Spring was starting to complain about missing parameters on the annotated method. What?
Ok, I was aware that Scala generates corresponding methods for a field just like Groovy does. But what was the issue here? The Spring annotation is defined as field level and method level annotation. But when it is used on a method it has to be a setter which gets at least one parameter. The decompiler saved the day. It shows that Scala is applying the annotation three times. At the field, at the setter pollService_$eq() and the getter pollService(). The last causes the Spring exception. (For all Scala experts, the @BeanProperty annotation does not solve the problem)
The (ugly) workaround is to create the setter manually and annotate only this. Here is how:

var pollService: IPollService = _;

@Autowired
def setPollService(pollService: IPollService) = {this.pollService = pollService}

4. Switch of “Compile Scala files first” in IDEA

There is a small bug/inconvinience in the current Scala plugin of IntelliJ Idea. It occurs when you have a project with combined Java and Scala sources. Any Java class that is located in a test source folder will not compile. You will get an error message “ClassX is already defined as class ClassX”. To fix this you can simply change the Scala compiler settings. Go to Settings->Compiler->Scala and uncheck “Compile Scala Files First”. It is not perfect, as you might need to manually (Ctrl-Shift-F9) compile Scala sources that Java files depend on. But at least you can compile everything now.

5. Use Scalatest!
If you want to write tests in Scala try Scalatest. It shows perfectly what can be done in Scala. It enables different styles of testing like xUnit or BDD just by applying different traits. And it enables you to use TestNG in your Scala tests. This way you can just start tests in Scala as if they where Java, at least in Intellij Idea. Here is a small example how a Test can look like:

@Test def testCreateTopicPoll() = {
  val pollDto = preparePoll
  val pollId = pollService saveOrUpdateTopicPoll pollDto;
  pollId should not be null
}

Look at the last line. Isn’t that beautiful? This is real code. You would not need any comments to explain, what is under test here.

I can strongly recommend that you have a look into Scala. It is a very interesting language with some nice concepts. I hope that some of my lessons learned help you when doing your first steps. Then let me know what lessons you learned!

Be Sociable, Share!
  1. Do you use the Junit4Runner in Scala with Scalatest and IDEA?

    Stephan
    http://twitter.com/codemonkeyism

  2. Oliver says:

    Sounds pretty cool.

    I just need a 48h day.

  3. Joerg says:

    @Stephan,

    No I use the TestNG Integration. I use TestNG for Java too, so there was no reason to use JUnit4.
    Why do you ask?

    @Oli,
    Yes 48 hours should be the minimum 🙂

  4. Excellent idea to use a decompiler and thanks for sharing the link! In Scala, there’s quite a bit of stuff going on behind the scenes. If you read further on in “Programming in Scala” (page 392), you will find an alternative workaround for the effect you described in (3). It should be possible to write out the getter/setter pair and create the correct annotations to satisfy Spring. Haven’t tried it though. Still getting my feet wet with Scala…

    Cheers, Thomas

  5. Hossam Karim says:

    @Autowired{val required=true}
    private[this] var pollService: IPollService = _;

    Should work on 2.7.5

  6. Steven says:

    You’ve got some formatting issues on this page.

  1. There are no trackbacks for this post yet.

Leave a Reply