Is there a functional language which compiles to JS and allows usage of JS libraries?
Here are some of my points:
- TypeScript is based on an amazing concept of being a superset of JS, which means that any valid JS code is also valid TS code, allowing usage of existing JS libraries.Now, TS is great, but I need a functional language which compiles to JS, and for which I do not have to sacrifice the JS libraries I already use.
- I use Elm to create web apps; let’s say I want to use Phaser and Box2D with Elm, I can’t do much except for passing on data back and forth.
- I thought about compiling Elm, or any other functional language like ClojureScript, PureScript, ElixirScript, to JS and merging it with my JS codebase, but that’s just painful. I am limited to only referring to the generated JS code from my hand-coded JS, but not the other way around.
Yes, OCaml (via Bucklescript). ReasonML is Facebook’s project that provides a JS-like syntax for OCaml and has the most traction at the minute.
Note that you still need to write bindings: with OCaml being a typed language, you can’t really get around this, because it will need to know what the functions you import are doing to be able to compile properly. But writing bindings is fairly trivial, and can be done on a per-function basis. OCaml is very good, I’d recommend it (though it has warts, in particular the documentation is…kinda bad).
Ocaml is a very sexy European car. It’s not quite as fast as C, but it never breaks down, so you end up going further in less time. However, because it’s French, none of the controls are in the usual places.
ReasonML is very much geared towards React at the minute, given that is Facebook’s use case — the web version of Messenger is mainly Reason, for example
ScalaJS has very nice support for JS interop. While it’s not a purely functional language like Elm or Purescript, it does support Cats and Scalaz, which are functional programming libraries. Like Ocaml/Bucklescript, you also have to write bindings, though unlike Ocaml it actually has decent documentation.