Saturday, 4 August 2012

Scala first impressions

In the last year or two I've been practicing TDD and the things that come naturally from it: dependency injection, inversion of control and data / behaviour separation. All these techniques complement each other and encourage you to write code that is actually quite functional. This lead me to start looking at functional languages to see if there was a better way to build software; OO paradigms were starting to look a little unnecessary. I started with Haskell, it being one of the most pure functional languages. I then tried F# and now I'm playing with Scala.

Scala is interesting because it is a full object-oriented language, in some ways going further than Java, and is also a functional language providing most of the things you'd expect from a functional language. For example:

  • Pattern matching
  • Higher order functions
  • Anonymous functions / lambdas
  • Type inference
  • Currying

Traits

One of the more interesting features of Scala is traits. These are similar to interfaces in Java and C# except that they allow implementations of the methods they describe. I find this a bit scary but also fascinating because a very common code smell I see is people deriving from base classes in order to re-use code between classes and not because of an inherent 'is-a' relationship. Traits let you re-use code between classes without a cumbersome inheritance relationship. I'm only starting to get the hang of them though, for example they can be stacked.

Pattern matching

Pattern matching in Scala is very similar to F# and Haskell. It's one of those features that you find yourself missing from other languages once you've learnt about it.

Type inference

The type inference in Scala is not as good as in F#, this is apparently because of Scala's OO features. That said it's still much better than Java or C#, although that's probably an unfair judgement).

Anonymous functions

This isn't really exclusively the domain of functional languages anymore, C# and even C++ support it now. I think the Java committee have really let themselves down by not including this in Java 7. They are extremely useful and pop up in all sorts of places. They also allow library writers to design great APIs.

JVM

I really like that Scala compiles to bytecode and can be run on lots of platforms. I love C# and the .NET framework, but I hate that it doesn't have first class support on non-windows platforms. The mono framework is an amazing piece of kit, but I'm always going to trust the JVM on Linux over Mono. That's perhaps unfair, but you really don't want to worry about bugs in your runtime environment and there's always a manager you're going to need to convince.

Tooling

One of the things that I found a little disappointing about the Haskell eco-system is that there is a distinct lack of decent tools. The only proper Haskell IDE is Leksah which is a step in the right direction, but is still in its infancy. Haskell is the perfect target for auto-refactoring tools because it has proper strong typing, but there isn't anything serious, no re-sharper or eclipse for Haskell. F# is better but still limited (you can't organise your code in folders in Visual Studio for example).

I've been really impressed with the tools available for Scala. There is decent support available in Intellij and Eclipse (through plugins). They're still a little rough, but are definitely usable.

I think Maven is fantastic and being able to use it to build Scala and pull in maven dependencies is wonderful. Also, SBT is an alternative to Maven for Scala which is easier to get started with (not being XML based helps), which I also quite like. SBT can pull in maven dependencies which means getting an SBT project started is very quick.

Conclusion

I'm clearly becoming a fan of Scala, but I can't help worry that the reason I'm getting on with it is because it's not actually very different from the languages I'm comfortable with (C#, C++, Python, Java etc). I think I still need to keep learning Haskell to try and get around some of my problems with it (like how to build a large system TDDing as I go along - I can't rely on IOC to help me out like I do with C# and Java). However, right now I would be happy to start a production project using Scala. Though I would love to do one in Haskell as an experiment, it would be a risky proposition.

No comments: