Rational Numbers in Scala
I just finished another chapter in the Programming in Scala book: "Functional objects". C++ strikes back: The return of operator overloading...
Todays chapter of Programming in Scala introduced "functional objects", in other words: immutables. The task was to implement rational numbers in Scala. Here are my highlights.
Conciseness: Scala is not Cobol
The language is concise (as advertised). Not like Cobol (shudder) nor like Ada. Static types are only required in a few places, mainly as parameter types. Type inference works well, saves some typing, and (more important) removes clutter from the code. The declaration of method return type is optional, but improves readability. The return keyword is also optional which also improves readability for this class, which has only very short methods.
For example the multiplication method:
// part of class Rational
def multiply(that: Rational) =
new Rational(this.numer * that.denom, this.denom * that.denom)
You can call that method either Java-like:
val result = new Rational(1,3).multiply(new Rational(3,5))
or with infix notation
val result = new Rational(1,3) multiply new Rational(3,5)
Operator overloading is also possible and useful for this example:
// part of class Rational
def *(that: Rational): Rational = this multiply that
Now multiplication can be written more naturally:
val result = new Rational(1,3) * new Rational(3,5)
Development Enviroment
I use the Eclipse plugin for Scala (version 2.7.2RC5). It is usable but rather limited in functionality compared to the Java development tools. There are lots of bugs: incremental compilation not properly synchronizing with the editors, incorrect diagnostics, and more. Syntax highlighting is OK, completion uses the static type information and is quite useful for novices like me.
Integration with Java
As expected you can call Java code without problems. After defining my Rational class I wondered how this would be usable from Java code. First I used javap to check the signatures of the class. Type definition and constructors look like Java, same for "conventional" methods as the "multiply" method above. The class implements ScalaObject, that means you have to include the scala jars on the classpath. Operator overloading results in methods with dollar signs in their names. Nevertheless they are callable from Java.
Actually it is fun to mix the different JVM languages in the same workspace. I implemented the class in Scala. I wrote the unit test in Scala but using the JUnit framework, because I do not know the testing framework of Scala. I did some additional tests in Groovy. Looking back at the C/C++ world this is really awesome: You can mix and match languages almost seamlessly. No core dumps, yet :-)
Conclusion
Todays lesson was another step up the Scala. No major obstacles encountered. Writing this blog entry took longer than implementing Rational with some JUnit tests. Of course I had the book as cheat sheet.
I can fully recommend "Programming in Scala" (at least from the first six chapters I have read). This book by Martin Odersky, Lex Spoon and Bill Venners is one of the best Programming Language introductions I have read. The examples are well chosen. There are few concepts that are used before described, and always with forward reference to the chapter where they will be introduced in depth. The book is available as e-Book (no DRM, but with watermark). The printed version can be pre-ordered and is due this month.
tags:















