Thursday, January 26, 2012

Simplifying scala scripts

I've already written several dozens of scala scripts (series computations, ssh automation, various jmx operations, remote administration, garbage collector log analysis, ...) and found this language quite interesting for use as a script language. There's many reasons for that :

Script automatic compilation reduce runtime error. I am often amazed at the first attempt to get a script that works and without any runtime error !

Take benefits of scala powerfull collections that make possible to write "sql" like operations

It becomes straightforward to parallelize tasks using Actors; one of my favorite use case is a short script that trigger an explicit garbage collection on several dozens of remote jvm in a very short time

But I miss some features that will help to make scala scripts even simpler and concise :

A #include like feature within script

A way to modify default imports, to avoid adding always the same imports in all scripts

the #! !# shell scala bootstrap can become long (and not DRY) once you want to add many external java dependencies

In fact those missing features are no so difficult to implement, the following source code is a proof of concept that shows it no so difficult to implement those features. It defines a class, Bootstrap, which can be use to start a scala script and that will bring new imports and definitions to your script.

Thanks to the assembly SBT plugin, you've generated a standalone executable jar, which contains the scala compiler, and our custom scala script startup mechanism.
In a next POST, I'll describe more in detail a new bootstrap implementation that will bring #include feature to scala script.