News for hackers. Stuff that matters.
Re:JSCaml: A compile-time transformation from JavaScript to ... by jordwalke on Tuesday March 21, 2017 @10:56PM (#13928162)
Hi I'm Jordan and I work on Reason(http://facebook.github.io/reason), and previously worked on ReactJS(https://facebook.github.io/react/), so this touches on a lot of things that are of interest to me. We currently use the OCaml language with the Reason frontend, for UI development, and use BuckleScript for compiling it to JavaScript. To some it might seem like this JSCaml project might be "backwards" in that it compiles from JS to OCaml, instead of the other way around, but both of these share one important / valuable thing in common - they use exactly one memory system, allocator, collector between two different languages. This is very different than embedding a separate VM/language/allocator as a library inside of another language runtime. If you've ever tried to bridge two completely different memory systems together without introducing leaks, you'll quickly realize why JSCaml gets the most important thing right, even if it is missing some JS language features - those missing features can be added over time, whereas unifying two inherently incompatible memory systems can be bankruptingly expensive.

Seamless memory lifetime interop without leaked cycles is essential for incrementally moving a code base from one language to the other. In browsers, that means your only hope is to target semi-idiomatic JavaScript if you want to incrementally move a large JavaScript system to another language. Wasm is currently great for whole-program rewrites from scratch, not incrementally moving over. That's why BuckleScript has been so helpful.

When not running in the browser, you have many more options, and if given the choice between running Reason/OCaml in a JavaScript VM, vs. running JavaScript in an ocamlopt runtime, the later offers many compelling advantages.

1. The ocamlopt runtime allows languages with sound static guarantees to take advantage of those guarantees to emit more efficient machine code, reaching the language's full potential. JavaScript will have to go through all the same dynamic (costly) checks due to its language complexity - at least for object property/method dispatch, but why should that mean that OCaml should have to as well? By running inside of the ocamlopt runtime, it can share one memory system without being limited by an unrelated dynamic language's weaknesses.

2. Depending on the approach, the emitted JavaScript might be able to take advantage of many of ocamlopt's ahead of time optimizations to reduce allocations, and inline function calls (F-lambda for example) without having to wait for the JIT to reach many of the same conclusions (which slows down startup time).

3. Even if the JavaScript compiled to ocamlopt runtime doesn't demonstrate as much throughput as it would with a JIT (once you wait for it to warm up of course), the approach of JSCaml allows you to seamlessly break off bottlenecks and write them in Reason/OCaml which could end up running even faster than JS with a JIT once warmed up (and without having to wait for the slow JS VM's startup initialization).

I'm sure there's a ton of work left on JSCaml until you can one-click deploy your JS programs and see performance wins, but I'm very interested in this general direction, as it has many uniquely compelling advantages that stand to help move the JavaScript ecosystem forward.

Re:JSCaml: A compile-time transformation from JavaScript to ... by runT1ME on Wednesday March 22, 2017 @01:23AM (#13928651)
I quite enjoyed playing with Reason and would love to get a chance to use it for something at work. We use a ton of Scala on the backend here at Verizon Labs (so many of us are into hardcore FP) but we occasionally run into GC issues, so the OCaml runtime is something that could potentially mitigate that.

Can you talk about what FB uses Reason to develop, and what kind of services are being built with it?

Re:JSCaml: A compile-time transformation from JavaScript to ... by ewrcoffee on Wednesday March 22, 2017 @01:16AM (#13928629)
Very interesting! Where can I more details in building UI with Reason?
Re:JSCaml: A compile-time transformation from JavaScript to ... by cies on Wednesday March 22, 2017 @05:12AM (#13929472)
It's all quite in early stage, but this is an example (UI in React) that speaks pretty much for itself:

https://github.com/chenglou/reason-react-example/tree/master...

This code is both very pretty and very terse, in my eyes.

Re:JSCaml: A compile-time transformation from JavaScript to ... by fineline on Wednesday March 22, 2017 @05:39PM (#13935254)
Hi Jordan, a bit off topic and late, but do you have any update on chenglou's comment on this thread:

https://news.ycombinator.com/item?id=12400397

that React bindings for Reason would be published on HN in a few days. I know there has been a lot of work in the repos for Reason for JSX support etc, but is there a public story about this yet anywhere that I've missed?

Thanks for any more info.

Re:JSCaml: A compile-time transformation from JavaScript to ... by jordwalke on Wednesday March 22, 2017 @09:07PM (#13936633)
Hi, thanks for your interest.

You can check out the development here: https://github.com/chenglou/reason-react-example

Cheng also gave a talk at ReactConf about our efforts.

Things are still quite in flux, but you're welcome to contribute and help improve things.