Saturday, 13 November 2010

Building Scala applications with SBT + Idea

Intellij IDEA is a great tool for Scala Developers. A couple of days ago I attended a great event of the London Scala User Group where, Richard Hallaway, Phill Wills and Maciej did a very good intro to this tool. This guide explains how to quickly leverage a couple of tools to setup a new Scala project in minutes.

First of all, start downloading right now the latest build of Idea X community edition for your platform, this will probably be ready by the time we finish with the setup. You'll also need the Scala and SBT plugin.

There are basically two ways to setup your idea projects with SBT. The first would be using Heiko Seerbergers guide to setup the project from Idea. I prefer to do it the way that Mikko Peltonen suggests... SBT-Idea.

First, you'll need the great SBT (Simple Build Tool), which is a build tool written in Scala and for Scala. Its power lies in that it relies on convention over configuration and giving you full control over your builds. No longer long XML configurations + Shell scripts. SBT can do everything from one single script. Furthermore, builds can pickup only the changes you made, and recompile and run only those tests. Check it out!

After you setup SBT  , create a new project:

Scala: 2.8.1
sbt: 0.7.4

this will create most of your project.

then exit the console, go to project folder, and create a folder build and inside, a scala file such as this:

Go back to the sbt console, type update, and it will pull the dependencies.

Use the SBT processor as specified here:

> *sbtIdeaRepo at
 > *idea is com.github.mpeltonen sbt-idea-processor 0.1-SNAPSHOT 
 > update
 > idea

this will create your project. Now, import in IDEA. All your SBT structure will be recognized.

Now let's setup the FSC (Scala Fast Compiler Server). SBT is great for quick reloads, but i still prefer to have fast compilation within the IDE.

For that, just go to Edit Configurations in your run targets in the toolbar, and create a new Scala Compilation Server using the following details. I had issues in the past when ticking the box shared configurations but it probably has been fixed already.

That's all you need ! Super fast compilation within Idea. And remember if you run your commands in sbt with a tilde ~ they will automatically run every time you switch back to the sbt console and it detects a file change.

Finally, don't forget your .gitignore


Scala 2.8.1 released

Plenty of bug fixes (more than 100 in 3 months!). Great to see a very active community, kicking the tires and gathering to solve problems. Furthermore, It's fully binary compatible with 2.8, which means no need to match library versions as it happened with the RCs for 2.8. Also, the improvements to Scaladocs make it look very snappy

All in all, we can see the the Scala language growing at a fast pace. Many new books are coming, including Phillip Haller's Actors in Scala and three others by Manning "in Action" series. Browse the complete list of books if you are interested.

Sunday, 31 October 2010

Scala just pushed Play in web development

Until recently, Scala only had Lift has a successful web framework. Meanwhile, the ruby gang has hundreds of them, more importantly ruby on rails, Sinatra and others. But things are changing fast in the scala world. While scalatra deals with Sinatra, there was nothing quite like ror. Enter Play Framework for Scala. Module history here

I must admit that this 11 pages guide to build from scratch an entire blog system, together with captcha, user administration console, open id authentication, using a clean RESTful interface, together with autogenerated unit, integration and selenium tests.... well, this is just absolutely fantastic. That's what i call a quality framework documentation! Scala just pushed Play > !

If you add free JRebel free licenses to Scala developers by ZeroTurnaround, Scala is automatically an ultra productive environment for web development!

Great effort! Can't wait for Play Framework 1.1!

Wednesday, 13 October 2010

Scala community growing ever faster

I'm becoming more involved with the Scala community and it looks very, very promising. The Scala Lift-Off event in London was a success, as demonstrated by richard's blog and maciek's notes on day 1  and day 2

Scala 2.8 was released in July, and already has an impressive list of bugfixes in 2.8.1. This demostrates the commitment from the community. Furthermore, Martin Odersky announced Scala Solutions, a new venture to offer commercial support and training.

The two biggest proposals of the event were Lift, which released the version 2.1 of their framework. Tim, who is writing a book on Lift made some really interesting comments. Particularly, I learnt about Squeryl, a fantastic framework to construct SQL queries in a friendly Scala DSL syntax, offering full compiler support ... and more importantly, refactoring.

Akka, the concurrency framework is close to the much awaited 1.0 release. Jonas Boner has made many presentations since then, including one at LinkedIn , which, by the way, uses Scala intensively with both Norbert and Scalatra, a Sinatra-like web framework in Scala

Graham Tackley, and ex-colleague of mine, did a nice presentation on The Guardian move to (and love of) Scala

Videos are available here

For great presentations, including some upcoming features in 2.9, have a look at the  Scala Days 2010 website. Parallel collections, scala refactorings for Eclipse, and much more.

Exciting days for the Scala community !

In the meantime, the demand for Scala in my current job is growing exponentially, so I'm currently preparing some presentations on the subject. Stay tuned!

King Java 7 is Dead, long live King Java 7!

FInally, after months years of discussions, Java 7 finally has a certain release date.  Fortunately, Plan B won, which means that all the available features are going to be shipped soon (mid 2011), and all the nice-to-have's including Project Lambda are delayed until Java 8, in late 2012.

This is great news for a community left in dire needs for an upgrade after almost 6 years (by the time of the release), and a half version (Java 6 update 10) which was shipped under the covers with many features pressing to be released.

This has huge implications for the Scala community, which can deliver today what Java would only be able to offer in 2013, while improving day by day thanks to the JVM 7. I can foresee a JVM increasingly more detached from the Java language itself, and Scala becoming a new powerful and widely accepted language.

Modular Java? with OSGI 4.2 available, why bother waiting?

Furthermore, IBM just announced that will be joining OpenJDK, ending years of dispute with the aborted Apache Harmony JDK.

So, good news for the JVM ecosystem.

Wednesday, 14 July 2010

Scala 2.8.0 FINAL released

The long awaited Scala 2.8.0 has been released today (just in time for my dissertation!)

The list of new features is gigantic, and thoroughly deserves a 3.0 release, but due to distribution issues, the version upgrade is just minor.

Most distinguished changes, the complete revamp of the collections API, named and default arguments, package objects, enhanced event based actors better documentation, thousands of bug fixes, Scala Eclipse IDE... too many to enumerate! Detailed changeset in the Scala website.

But most significantly, it means that all the scala libraries can now be compiled against a stable version, due to binary incompatibility even across minor releases.

This is a great day for the growing Scala community!

Sunday, 6 June 2010

Comparing functional programming with JDK7 Lambdas and Scala

It seems that since Oracle bought Sun, they want to start breathing new life into Java. Out of the blue, Project Lambda seems to be pushing the spec for what would end up been functional idioms for Java.

The debate that the new syntax brings is captured in this InfoQ article on project Lambda.

compare Java 7's:

with Scala's

for my part, i prefer Scala's functional syntax, since I have a first class function that i can invoke. Brian Goetz (Concurrency guru) suggests that at the moment they are focusing in functionality rather than syntax, and that it will evolve.

Now, they better work a lot in order to improve this spaghetti:

For a move in depth review of the changes proposed, read this analysis on Project Lambda in InfoQ

The interesting point to make out of this is that Functional Programming is becoming more and more important and Java doesn't want to miss out. A few months ago, JDK7 was leaving closures out of the spec in order to ship a highly demanded final version. But now Oracle is in command, and they want to include FP in Java. This means that effectively Functional Programming is now becoming mainstream, supported in almost every major platform: F# in .Net, Scala, python, javascript, groovy, and now Java.

Wednesday, 26 May 2010

Simple build configuration for Scala 2.8 with Maven

There are plenty of blogs explaining how to run Scala with Maven, but, unfortunately, many of them are out of them, no less the scala-lang website, which I will try to update with this simple recipe.

The good thing about this configuration, is that is tested with both Eclipse and IntelliJ Idea 9.0.2 (open source)

The directory structure remains the same as for java, with the obvious difference:


Finally, add your package and test it with a Canary Test

JUnit 4 + Hamcrest matchers seems like a very simple combination that will get the job done without dealing with the binary incompatibility problems of Scala before the final release of 2.8. It seems that jars need to be compiled against the exact version of the compiler or they won't work. I don't yet know enough about this problem, but was significant enough to drive me away of using the most recent versions of ScalaTest and Specs for now. Thus, I'm sticking to
2.8.0.RC1 which seems to work in all IDEs.

At the moment i'm using Intellij Idea 9.0.2, which comes with a pretty good Scala plugin. Eclipse is usually my IDE of choice, but the plugin is in an early stage yet, since doesn't support any refactorings.

Another rather annoying problem was with Hamcrest, which doesn't have the 1.2 version available in Maven Repositories, but it comes with some nice matchers some I recommend you to download it from their website. It seems that the High priority bug to deploy it in Maven Repo Central has been open for... 3 years! I added the 35th request to get it closed :) Otherwise, just switch to 1.1 version, which is available from Maven Central.


Monday, 17 May 2010

Coding the architecture

Today I attended to a free seminar by Simon Brown of Coding the Architecture today at SkillsMatter, London. He delivers a two day day course on Architecture for developers, and gave a  sneak preview, focusing on non functional aspects, and architecture definition. Essentially, how do you go about writing an architecture from scratch. Overall was pretty solid, although he seems to favour RUP as an intermediate between Waterfall and Agile, but I tend to disagree, but I would be interested in hearing the full course to understand his argument.

If you are interested he also wrote a very interesting article on InfoQ... are you an architect? He helps to visualize the many dimensiones of the role, and he emphasizes many social aspects that are usually overlooked, such as how to communicate an architectural principle across the team or teams.

Sunday, 16 May 2010

The end of the line

Ever wondered how to move to the start or the end of the line in the terminal in your MacBook? Ctrl+A and Ctrl+E respectively.

It also works on the Scala REPL, where you can quickly evaluate code snippets. Some useful tips here

Even while you surely know, home, end, page up and down are all accessible by combining the Fn + arrow keys.

Another end of the line shortcut is Shift+G in less. To tail that buffer do Shift+F (Ctrl+C to stop).


Saturday, 15 May 2010

Hello Scala World

Simple and concise. The thing to notice here is the keyword object. Scala has the built in concept of a Singleton, which simplifies design. All the static members are to be placed in the object companion, which replaces enums and acts as a default factory for your object. Brilliant!

To do enums, we have several alternatives:


Which can be used extensively with case classes.

Thanks to Carte Cole's syntax highlighting tutorial using SyntaxHighlighter

Vim syntax highlighting for Scala

Scala offers the flexibility to execute from command line (scala -e), through its REPL, as a script, or accessing the conventional main class. While REPL can be useful for quick hacks, the script has the advantage that it can be used for good shell scripts. On my Mac OS X Snow Leopard, (although it works on any unix) I configured vim as such:
curl -o .vim/syntax/scala.vim

curl -o .vim/indent/scala.vim

curl -o .vim/ftdetect/scala.vim
then inside of vim, activate it:
:syntax on
Finally, to enable highlighting by default, add/modify your .vimrc and add the line
:syntax enable
Restart your console and you can now enjoy scala highlighting (although the color theme probably needs tweaking...)

Polyglot programmer

For years, Java has been considered 'the enterprise programming language', only contested by .Net. But since ruby on rails appeared, the development community started asking questions whether Java was the right choice in every case. A movement sparked by Andy Hunt and David Thomas following their book The Pragmatic Programmer led to the concept of Polyglot Programmer. They suggest that developers should 'speak' more than one language, and learn one every year, to stay fit, and adopt different paradigms such as functional programming (Lisp descendants such as Haskell) or the parallelism friendly Erlang.

Each language have pros and cons, but unlike Ruby, a handful of this new languages chose to profit from the large efforts invested on the JVM. It started off with
Groovy, a dynamic language that appealed Java developers due to its compatibility, Jython (Python interpreter), but moved on to JRuby, Scala and finally Clojure.

Scala has the added benefit that is binary compatible with Java, which means that is fully interoperable, and as such able to access its gigantic library.

From this year onwards, my goal is to become proficient in Scala, which I will be using for my dissertation. Stay tuned for more on Scala!