Justin du Coeur

This week's main project is to add scala.js to the Querki build. scala.js is -- well, let's put it this way. Querki is made up entirely of leading-edge technology. Some of the libraries qualify as bleeding-edge -- open-source bits that are very new and novel, but still decently mature.

scala.js is *hemorrhaging*-edge technology -- a project being run out of EPFL, the university where Scala was largely born in the first place. It's still officially pre-release, and has no commercial entity behind it. (Although a lot of us are starting to agitate for Typesafe to pull it in as an official piece of the Typesafe Stack.) But man, it is the coolest and most useful thing on Earth.

scala.js is a Scala-to-Javascript compiler. Everyone is beginning to acknowledge that Javascript is kind of a pain in the ass for serious work: while it is much more powerful than anyone understood at the beginning, it has all sorts of nasty limitations that you have to work around -- all sorts of table-stakes features like classes and modules and such have to be added as libraries, and all of it is dynamically typed, so you have to catch your errors at runtime. OTOH, those runtimes are now *blisteringly* fast, thanks to Google forcing everyone else to up their game, so you can basically treat it as machine code, and scads of languages are being produced that compile down to Javascript.

A couple of years ago, some folks decided to see whether it would be possible to compile Scala -- one of the most un-Javascript-like languages around -- to Javascript. It's taken a lot of releases and some very clever work, but the answer turns out to be yes. By now, scala.js works with the vast majority of the Scala language, as well as the most important libraries. And it's hit a point of maturity where a bunch of folks have been producing libraries that are specifically designed to be useful in the browser environment. (Big h/t here to Li Haoyi, who in his spare time has been doing some really amazing work.)

Anyway, I've now got that up and running, at least in test form. But today's "wow" moment was the tweak I just put in place, to get Source Maps working. This is a tech that Google has been pushing: basically, if you think of Javascript as machine code, this is the debugging symbols. The end result is that I can not only *write* my UI in Scala, I can (in Chrome, at least) *debug* it in Scala. I've now got things wired up so that I can, right in the browser, view the Scala source code for my client, set breakpoints in that code, inspect variables and parameters, the whole nine yards.

Of course, now I get to rewrite all of my existing Javascript code in Scala, which will be a mild PITA -- expect some bugs to sneak in while I iron that all out. But in the long run, this should make Querki's UI both more powerful and easier to develop. Javascript has been the biggest pain point in my programming life for many years now (I actually learned Flash in order to give myself an alternative to it), so the notion of being able to write Querki in end-to-end Scala fills me with absolute glee...

Source maps are really cool. I keep meaning to play around with that more, given all my JS development is really CoffeeScript. And they are pretty 1-1--I can only imagine how hard it would be to try to figure out the Scala source of an error.

Oh, worse than you'd imagine. Suffice it to say, Scala is a long, long ways from 1-1. Indeed, you have to explicitly export symbols that should be visible to Javascript, because by default they're quite munged.

And that's in the "nice" view of the generated code. Of course, when you run it through the full optimizer, which drops the code size by 80-90%, it turns into truly exceptional gobbledygook. But it runs quite well: when fully optimized, the benchmarks are currently saying that it's typically better than half the speed of hand-coded Javascript, and they're pushing to achieve parity on average...