Monday, February 06, 2012

Scala Again

I am trying Scala again. Last time, several years ago, I played around with it as a web tool, combining it with a Servlet Runner like Tomcat. This time, I play around with it for some quantitative finance experiments.

Why Scala? It still seem the most advanced alternative to Java on the JVM, and the mix of functional programming and OO programming is interesting. Furthermore it goes quite far as it ships with its own library. I was curious to see if I could express some things better with Scala.

Here are my first impressions after a week:
  • I like the object keyword. It avoids the messy singleton pattern, or the classes with many static methods. I think it makes things much cleaner to not use static at all but distinguish between object & class.
  • I like the Array[Double], and especially ArrayBuffer[Double]. Finally we don't have to worry between the Double and double performance issues.
  • I was a bit annoyed by a(i) instead of a[i] but it makes sense. I wonder if there is a performance implication for arrays, hopefully not.
  • I like the real properties, automatic getter/setter: less boilerplate code, less getThis(), setThat(toto).
  • Very natural interaction with Java libraries.
  • I found a good use of case classes (to my surprise): typically an enum that can have some well defined parameters, and that you don't want to make a class (because it's not). My use case was to define boundaries of a spline.
  • I love the formatter in the scala (eclipse) IDE. Finally a formatter in eclipse that does not produce crap.
Now things I still need time to get used to:
  • member variable declared implicitly in the constructor. I first made the mistake (still?) to declare some variables twice.
  • I got hit by starting a line with a + instead of ending with a +. It is dangerous, but it certainly makes the code more consistent.
  • Performance impacts: I will need to take a look at the bytecode for some scala constructs to really understand the performance impact of some uses. For example I tend to use while loops instead of for comprehension after some scary post of the Twitter guys about for comprehension. But at first, it looks as fast as Java.
  • I wrote my code a bit fast. I am sure I could make use of more Scala features.
  • The scala IDE in eclipse 3.7.1 has known issues. I wish it was a bit more functional, but it's quite ok (search for references works, renaming works to some extent).
  • Scala unit tests: I used scala tests, but it seems a bit funny at first. Also I am not convinced by the syntax that avoid method names and prefer test("test name"). It makes it more difficult to browse the source code.
Some things they should consider:
  • Integrate directly a Log API. I just use SLF4J without any scala wrapper, but it feels like it should be part of the standard API (even if that did not work out so well for Sun).
  • Double.Epsilon is not the machine epsilon: very strange. I found out somewhere else there was the machine epsilon, don't remember where because I ended up just making a small object.
  • Unit tests should be part of the standard API.
Overall I found it quite exciting as there are definitely new ways to solve problems. It was a while since I had been excited with actual coding.

2 comments :

  1. On Double.Epsilon,
    https://issues.scala-lang.org/browse/SI-3791

    But when you mention machine epsilon, doesn't it remind you of java's strictfp? The machine epsilon should not be used if you assume a portable JVM.

    ReplyDelete
  2. Hello,

    Some comments on your post:

    * if you really do care about performance on int/double/etc on tight loop, there was a lot of discussion on the ml, see for example: http://groups.google.com/group/scala-user/browse_thread/thread/86adb44d72ef4498 and other messages from Rex Kerr and smael Juma. You would also want to know that scalac -optimise option is doing great progress nowadays, that there is something called "specialisation" that can help, and that the inliner progress coming in 2.10 seems quite astonishing (see the scala-internals googlegroups recent mails about that)

    * for the logging library, I'm really happy with the trait comming with Lift-common library (independent from Lift-the-webapp) [1] . It allows to say "class Foo extends Bar with Loggable", and that all: now, you can use "logger.debug(...)" in your class, and it's based on SLF4J.

    * Unit tests are something with far to many variations to be in a standard library :) Just for Scala, there exists a dozen test frameworks, going from the thin layer atop JUnit to things not known in Java like ScalaCheck, passing to BDD tests, specifications, etc.

    And for my personnal point of view about Scala: refactoring is just so good and easy when the compiler complain for all place where you have to change things :)

    Cheers, and good luck with Scala

    [1] https://github.com/lift/framework/blob/master/core/common/src/main/scala/net/liftweb/common/Logging.scala

    ReplyDelete