ReasonML vs Elm [closed]
Asked Answered
C

1

34

I saw this ReasonML vs TypeScript question here at StackOverflow, now I'm wondering how ReasonML and Elm compare to each other.

What are their similarities and differences? Which one should I use when? What's the advantage of one over the other?

Casein answered 25/6, 2018 at 3:14 Comment(5)
@OnorioCatenacci this question would absolutely be closed if it was tagged javascript. But I've noticed that broad elm often remain open, and even get good answers (case in point, @glennsl's answer)Founder
Still an extremely broad question, tagged with javascript or not. How can one ask "Which one should I use when?" (in reference to any two languages) and not have a lot of opinions generated?Margalit
This isn't exactly a good fit for StackOverflow. But, it's an interesting addition (though broad, nonetheless) so I'm letting it pass.Annora
Its fairly explicitly a bad question for SO and its original natural home (programmers) is no longer available (because its software engineering) which is a shame because its an interesting question. The simple answer (which I can't give because...) is that Reason is a general purpose language and Elm is very clearly focused on front end client applications and takes that constraint as a positive (and reflects that purpose is its own constraints).Lauretta
Note the similarity to this other still open question: #18405658Observable
P
86

I'm not intimately familiar with Elm, but I've looked into it a bit and I'm pretty familiar with Reason, so I'll give it a shot. I'm sure there will be inaccuracies here though, so please don't take anything I say as fact, but use it instead as pointers for what to look into in more detail yourself if it matters to you.

Both Elm and Reason are ML-like languages with very similar programming models, so I'll focus on the differences.

Syntax:

Elm uses a Haskell-like syntax that is designed (and/or evolved) for the programming model both Elm and Reason uses, so should work very well for reading and writing idiomatic code once you`re familiar with it, but will seem very different and unfamiliar to most programmers.

Reason tries to be more approachable by emulating JavaScript's syntax as much as possible, which will be familiar to most programmers. However it also aims to support the entire feature set of the underlying OCaml language, which makes some functional patterns quite awkward.

One example of this is the function application syntax, which in Elm emphasizes the curried nature of functions (f a b) and works very well for composing functions and building readable DSLs. Reason's parenthesized syntax (f(a, b)) hides this complexity, which makes it easier to get into (until you accidentally trip on it, since it's of course still different underneath), but makes heavy use of function composition a mess of parentheses.

Mutability:

Elm is a purely functional language, which is great in theory but challenging in practice since the surrounding world cares little about Elm's quest for purity. Elm's preferred solution to this, I think, is to isolate the impurity by writing the offending code in JavaScript instead, and to then access it in Elm through either web components or ports. This means you might have to maintain significant amounts of code in a separate and very unsafe language, quite a bit of boilerplate to connect them, as well as having to figure out how to fit the round things though the square holes of ports and such in the first place.

Reason on the other hand is... pragmatic, as I like to call it. You sacrifice some safety, ideals and long-term benefits for increased productivity and short-term benefits. Isolating impurity is still good practice in Reason, but you're inevitably going to take short-cuts just to get things done, and that is going to bite you later.

But even if you do manage to be disciplined enough to isolate all impurity, you still have to pay a price to have mutation in the language. Part of that price is what's called the value restriction, which you're going to run into sooner or later, and it's going to both confuse and infuriate you, since it will reject code that intuitively should work, just because the compiler is unable to prove that there can`t at some point be a mutable reference involved.

JavaScript interoperability:

As mentioned above, Elm provides the ability to interoperate with JavaScript through ports and web components, which are deliberately quite limited. You used to be able to use native modules, which offered much more flexibility (and ability to shoot yourself in the foot), but that possibility is going away (for the plebs at least), a move that has not been uncontroversial (but also shouldn't be all that surprising given the philosophy). Read more about this change here

Reason, or rather BuckleScript, provides a rich set of primitives to be able bind directly to JavaScript, and very often produce an idiomatic Reason interface without needing to write any glue code. And while not very intuitive, it's pretty easy to do once you grok it. It's also easy to get it wrong and have it blow up in your face at some random point later, however. Whatever glue code you do have to write to provide a nice idiomatic API can be written in Reason, with all its safety guarantees, instead of having to write unsafe JavaScript.

Ecosystem:

As a consequence of Elm's limited JavaScript interoperability, the ecosystem is rather small. There aren't a whole lot of good quality third party JavaScript libraries that provide web components, and doing it yourself takes a lot of effort. So you'll instead see libraries being implemented directly in Elm itself, which takes even more effort, of course, but will often result in higher quality since they're specifically designed for Elm.

Tooling:

Elm is famous for its great error messages. Reason to a large degree does not, though it strives to. This is at least partly because Reason is not itself a compiler but instead built on top of the OCaml compiler, so the information available is limited, and the surface area of possible errors very large. But they're also not as well thought through.

Elm also has a great packaging tool which sets everything up for you and even checks whether the interface of a package you're publishing has changed and that the version bump corresponds to semantic versioning. Resaon/BuckleScript just uses npm and requires you to manage everything Reason/BuckleScript-specific manually like updating bsconfig.json with new dependencies.

Reason, BuckleScript, its build system, and OCaml are all blazing fast though. I've yet to experience any project taking more than 3 seconds to compile from scratch, including all dependencies, and incremental compilation usually takes only milliseconds (though this isn't entirely without cost to user-friendliness). Elm, as I understand it, is not quite as performant.

Elm and Reason both have formatting tools, but Reason-formatted code is of significantly poorer quality (though slowly improving). I think this is largely because of the vastly more complex syntax it has to deal with.

Maturity and decay:

Reason, being built on OCaml, has roots going back more than 20 years. That means it has a solid foundation that's been battle-tested and proven to work over a long period of time. Furthermore, it's a language largely developed by academics, which means a feature might take a while to get implemented, but when it does get in it's rock solid because it's grounded in theory and possibly even formally proven. On the downside, its age and experimental nature also means it's gathered a bit of cruft that`s difficult to get rid of.

Elm on the other hand, being relatively new and less bureaucratically managed, can move faster and is not afraid of breaking with the past. That makes for a slimmer and more coherent, but also has a less powerful type system.

Portability:

Elm compiles to JavaScript, which in itself is quite portable, but is currently restricted to the browser, and even more so to the Elm Architecture. This is a choice, and it wouldn't be too difficult to target node or platforms. But the argument against it is, as I understand it, that it would divert focus, thereby making it less excellent at its niche

Reason, being based on OCaml, actually targets native machine code and bytecode first and foremost, but also has a JavaScript compiler (or two) that enables it to target browsers, node, electron, react native, and even the ability to compile into a unikernel. Windows support is supposedly a bit sketchy though. As an ecosystem, Reason targets React first and foremost, but also has libraries allowing the Elm Architecture to be used quite naturally

Governance:

Elm is designed and developed by a single person who's able to clearly communicate his goals and reasoning and who's being paid to work on it full-time. This makes for a coherent and well-designed end product, but development is slow, and the bus factor might make investment difficult.

Reason's story is a bit more complex, as it's more of an umbrella name for a collection of projects.

OCaml is managed, designed and developed in the open, largely by academics but also by developers sponsored by various foundations and commercial backers.

BuckleScript, a JavaScript compiler that derives from the OCaml compiler, is developed by a single developer whose goals and employment situation is unclear, and who does not bother to explain his reasoning or decisions. Development is technically more open in that PRs are accepted, but the lack of explanation and obtuse codebase makes it effectively closed development. Unfortunately this does not lead to a particularly coherent design either, and the bus factor might make investment difficult here as well.

Reason itself, and ReasonReact, is managed by Facebook. PRs are welcomed, and a significant amount of Reason development is driven by outsiders, but most decisions seem to be made in a back room somewhere. PRs to ReasonReact, beyond trivial typo fixes and such, are often rejected, probably for good reason but usually with little explanation. A better design will then typically emerge from the back room sometime later.

Penicillate answered 25/6, 2018 at 15:43 Comment(5)
I thought bucklescript was backed by bloomberg?Bogart
The repository has been moved from Bloomberg into a separate organization, the developer is, as I understand it, no longer employed by Bloomberg and has moved from NY back to China. And Bloomberg is not even listed as a user on the BuckleScript website. So I don't think they're doing much backing anymore, no.Penicillate
Cheers appreciate your answer. I'd add that Elm is very performant in terms of DOM manipulation, but in terms of being quick to compile - I can't comment, I haven't benchmarked nor have I reviewed the literature on this. Unless benchmarked it will be difficult to assess definitely.Andantino
@BKSpurgeon Elm 0.19 improved compile-time significantly. BuckleScript is still much faster, but just from subjective experience I've had no issue with compilation speed on a 10k line Elm 0.19 project at least.Penicillate
Great post! Having some experience with Elm, I do disagree on one point though, which is the bureaucracy: Elm is a small project, basically run by a single guy. But has the seriously impressive ability to simulate the dysfunction of a calcified global consortium. It’s a one-man IBM-level disaster of constantly renaming github organizations, shuffling of projects, merging, splitting, closing-won’t fix, closing-fix-but-didn’t, and ignoring issues, deprecation of features that were headliners for the previous release, Removing needed functionality before the replacement exists (websockets) etcBrunhilde

© 2022 - 2024 — McMap. All rights reserved.