At this years’ Scala Exchange keynote, Jessica Kerr got a nice round of applause when she asked us all to, please, include our import statements when sharing code snippets.
That’s great advice – but can’t we do better? What if we could share actual running, hackable code?
Turns out we can: there’s a number of ‘JSFiddle‘-style projects around, and I’ll take a brief look at some of them.
Scala-Js-Fiddle leverages Scala.js: once compiled on the server, your Scala application runs in the browser.
This allows you to create beautiful visuals while running your applications. This could be an awesome teaching tool. Be sure to check out their documentation, which comes generated on-the-fly by a Scala snippet!
The downside of using Scala.js is of course that you’re restricted in which libraries you can include.
Scripster by Razvan Cojocaru (github) allows you to write Scala code in a syntax-highlighted environment, and gives some control over which expressions are evaluated.
Even though this tool is not restricted to Scala.js, only a limited number of libraries is included.
Scala Kata by Guillaume Massé (github) is an attractive editor with completion features, which feels much smoother than Scripster.
While again you don’t have control over which dependencies are available, a nice collection is included.
Unlike other options Scala Kata does not show the output of the program, but the value of the expression on each line, which might be familiar if you’ve ever worked with the ‘notebook’ feature of Scala IDE. While sometimes constraining, this encourages a nicely functional style.
Scala Kata is currently my online Scala environment of choice, though unfortunately it doesn’t provide an easy way to store and share your snippets.
Scala Notebook looks pretty clean, but unfortunately does not appear to be available in hosted form anywhere.
The project takes its inspiration from the IPython notebook, which is indeed a great example.
An interesting aspect of Scala Notebook is that, somewhat like scala-js-fiddle, it allows rich output: when a command returns HTML or an image, it will be rendered/shown.
Users of scastie will have to do without fancy features such as advanced highlighting and completion, but gives you complete control over the sbt configuration – making it a very powerful alternative.
|intermediate values||completion||git(hub)||dependencies||scala version|
|Scala Notebook||yes||no||no||full control|
Some desirable features, like a more ‘direct’ REPL-like interface, are not widely supported. Also, none of the available offerings appear to be particularly easy to embed into an existing site or blog.
If these services would become wildly popular, I wonder whether making sure there is sufficient back-end computing power available might become a challenge. On the other hand, computing is rapidly becoming cheaper, so this might not be an issue after all.
I was amazed by the number of projects that managed to make a functional Scala programming environment available in the browser. While there’s more work to be done, there is amazing potential here.