Compile To JS

A lot of people have made some really interesting points regarding Isaac's post on javascript not being the assembly of the web.

For the record, I'm on his side. I like writing javascript, I feel like it is expressive and concise, and I don't feel like any of to compile-to-js languages add enough for the compile step disconnect to be worth it.

But it did get me thinking about what it would take for me to use a compile to js language.

I think the only reason to use a higher level language is that it uses more abstract metaphors to simplify underlying concepts. For me, syntax is an aesthetic thing, and although I care deeply about the beauty of the code I write, syntax alone does not determine a language's efficacy.*

So what would a language need to replace js for me? And what would be nice to have?

1. Improved abstraction around inheritance

I like prototypal inheritance a lot. I always disliked the rigidity that a class-only based inheritance structure imposed. The conceptual simplicity of prototypal inheritance is extremely attractive. And yet in practise, using javascripts prototypal inheritance is messy. There's pointers you have to override (prototype.constructor etc.), people will forget to use a 'new' keyword. In fact it's such a mess that people have written libraries to make javascript similar to models that exist elsewhere.

A language that had a clearer model could do well here. I don't think that the syntax changes put forward by coffeescript, or even ES.next go far enough.

2. Improved efficiency

The only way to make a computer faster is to make it do less. The various JS interpreters are already incredible pieces of technology, but a compile to js language would have the ability to improve efficiency.

The reason higher level languages can be made faster is that they are a representation closer to what a programmer is trying to do, and further from how they are trying to do it. Behind the scenes, all sorts of magic can be used to collapse that down into less work for the computer.

A compile-to-js language could have a deep knowledge of the javascript interpreters available, and optimise for them - counting arguments to functions and inlining for example. Because by its nature, javascript is JIT compliled, a language with a defined compile step could take advantage of tricks unavailable to the js interpreter.

3. Optimise for a platform

Isaacs touched on this in his post, but high level languages usually can be compiled to a target platform. While coffeescript tries to remain interchangeable with js, and thus has to compile to all platforms, a higher level language could be targeted to an interpreter, thus you could tailor a version to the v8 engine and have it run faster in node.js. You could then compile it to good browsers which would tailor it to standards compliant browsers.

This would be nice, especially if DOM inconsistencies were taken care of in the browser environments, because a huge amount of time in web programming is spent dealing with browser differences, it would be amazing to make this a compiler problem.

4. Better Number Support

Javascript is not great for doing math(s). The ability to use real integers, floating point, and fixed point numbers would be pretty great. Especially as we are gaining the ability to use the GPU for canvas drawing. A higher level language could interface with the GPU for complex math etc...

5. Abstraction Libraries for Web Constructs

Many of the things that are now available to us in the browser were written with the constraints of the web foremost, and the usability of, second. Things like web workers, or IndexedDB are extremely powerful, but require a certain level of forethought. It would be awesome to be able to delegate callbacks to a web worker on the fly, for example. A high level language could do interesting things here.

So Much More...

In fact, as I started writing, there's a lot of things off the top of my head that a higher level language could address - those were just the first 5 I thought of.

Now some people may argue that some of these are being addressed in ES.next or in the browsers - but we are going to have to support older browsers for a while. Perhaps someone will write a compiler for ES.next to transcribe it into old js so that new code can be run in older browsers also.


*1. I wrote python for around 5 years, and I still think python is one of the most expressive and beautiful languages out there, but today I've transitioned almost completely to javascript. I like the syntax a whole lot less, but the metaphors are clearer for me in javascript. Callbacks are a more natural way of expressing asynchronous programming than coroutines.