Scala… As good as it gets…

As good as it gets was a film starring Jack Nicholson, in which he played a misathropic writer who also happened to suffer from OCD. What bearing has this got with Scala ? Well non-directly, or even indirectly TBH, though I’d say that if Scala were best represented by any psychological disorder, it would most likely be some flavour of bipolar disorder. Scala really does seem to be a modern programming Marmite, where developers either love it or hate it. With no real middle ground being forged between the two. I personally find this quite ironic given the ‘maleability’ of Scala as a programming tool.

Over the next few weeks (or more likely months), I hope to give an overview of the pros and ‘cons‘ [terrible pun intended] of my experience with learning Scala. From the off, it’s fair to say that, so far, I quite like Scala. I’m not sure I understand all, (or even most) of it, but the more I grok, the more I like. (I actually suspect I like it as result of finding it such a challenge, but enough of my S&M tendencies !). Also, it’s worth adding, (for context), that I’m fairly new to functional programming, and have spent about the last decade of my professional career writing ‘enterprise‘ (or more accurately non-trivial) Java code.

Scala has been around for several years, with this year marking a milestone in the language, and (moreover) the community which surround it, with the inaugural ScalaDays conference held in the Scala fatherland of Luasanne, Switzerland.

So, to get the ball rolling what is Scala ? And why is it of interest, either technically or commercially ?

Scala is a static typed, multiparadigm, JVM (and soon to be CLR) based programming language, which tries to blend OO and functional concepts together into a cohesive environment [sic]. The name itself comes from a concatenation of the words Scalable and Language, though (strangely), it is suggested the name be pronounced as ‘Scah-la’ (as opposed to ‘Skay-lar’). It’s apt that Scala originated in Switzerland, as it’s a real Swiss army knife of a language. Given it’s cross paradigm nature, there are strong intimations here that the programmer shouldn’t have to, (i.e. ‘need‘ to), pick their implementation language based on the problem at hand, as Scala (more than likely) can adapt to the problem. Also, the x-paradigm nature of the language enables a nice migration/transition path for developers from one paradigm to the other, or just those keen to broaden their knowledge. IMHO, there’s enough familiar to make the language approachable, and enough new *stuff* to pose a challenge. The language is statically typed, but heavily relies on type inference, hence removing much of boilerplate overhead typically incurred when using statically typed languages. As part of the ‘army knife’ range of capabilities on offer, Scala can be scripted and has a command line REPL interpreter, to allow for rapid prototyping, and testing of code snipets and scripts.

So, the first challenge to any developer mildly interested in Scala in what the business benefit the language provides ? and (equally importantly), how can and do you justify the language to a boss for use on a project or for training ? Also, what tangible features would convince our peers it’s actually worth looking into (the why of our triumvirate) ?

Well, as the language sits on top of the JVM (with compatibility efforts being renewed in the .Net world), any existing JVM based frameworks are readily consumed by Scala applications, as well as being Scala applicationas and libraries being available for consumption on those platforms. Hence, the language is complimentary to any existing JVM/CLR based investment. A common description of the language is ‘dense’ in that there is lots of syntactic sugar, making the actual ‘finger work‘ of development easier. This can be shown in some of more superficial features (such as the type inference implementation – requiring that types are only declared on the ‘right hand side‘ in variable declarations [if at all], and note having to end statements with semi-colon’s), to more powerful and fundamental/conceptual capabilities like case classes (which shortcut the typical style of Java Class declaration), implicits, (object based) pattern-matching, closures and all manner of  functional programming constructs, (e.g. top level closures, high order functions, partial functions and currying, and [soon to land] translucent functions ! – [All due to be covered in later posts !]),  which enable functionality not previously, (or at least readily), possible in Java. In theory this should make development with Scala quicker than the equivalent in Java. Given the myriad of developer resources geared towards productivity that are baked into the language, you’d expect development time to fall in line with these added capabilities, though my (limited) experience suggests more time is spent designing elegant/appropriate solutions (where you want to use the ‘right’ tool from the ‘right’ paradigm for the job), instead of in the actual monkey work of typing it in (still if you had to vest your time somewhere, what better place ?).

The OO representatation in Scala is much purer than that in Java, with ‘==’ performing logical comparison (i.e. equivalent of the equals() method in Java), and everything being an object (so no primitives in the language, though the compiler will optimise variables to primitives where applicable). What all this means is that if you’re engaged in just pure OO development in Scala then, turnaround times really *should* be quicker (versus in Java).

Scala heavily alludes towards using functional techniques in development, with immutable types being the default, and (as previously mentioned) functions being available as first class citizens of the language. As well as enabling easier thread safe programming (through immutable data types and an message passing style of concurrency using Actors – producing code which is is more adept at running in parallel across multiple processors), the functional support also allows for different programming strategies, such as using more reactive and continuation based programming patterns. These ‘features’ enabled by the language should really be enough to commodotise it’s use alone, and I’ll try and cover these in more depth in future posts.

So, to kick off, I’d suggest either visiting the Scala lang website (http://www.scala-lang.org/), and grabbing a copy of the compiler (the latest RC is for 2.8, but I’ll be working with 2.7.7 for the most part, and 2.8 when necessary), or grabbing the compiler from whatever repo might be at your disposal (for Linux based systems – 2.7 is available in the main Ubuntu repo). For the most part I’ll try and post code samples that can be easily authored in some form of textpad and run from the command line, though there’d be no harm in grabbing an install of the excellent Eclipse Scala IDE (http://scala-ide.org). I’ve also heard (anecdotally) that IntelliJ has excellent Scala support (http://plugins.intellij.net/plugin/?id=1347) with the capability to actually cut-and-paste Java code into the IDE and have it miraculously appear as equivalent Scala code.

Next time, Shortcut Domain Objects in Scala !

That’s it for now. Have fun, and happy hacking.

Leave a Reply