5 ms·
A couple of people have asked why you might choose OCaml over other languages. I've not done as much OCaml work as others on this thread (I work primarily on Re
by jordwalke 12y ago
A couple of people have asked why you might choose OCaml over other languages. I've not done as much OCaml work as others on this thread (I work primarily on ReactJS (Facebook/Instagram's) functional UI framework), but I can offer a different perspective as someone who is outside of the OCaml community, but asking the same questions. Here are some of my personal findings.
I'll narrow any comparison down to the ML family of languages. Java/C++/ and many other languages are just now beginning their slow, but inevitable evolution into becoming a dialect of ML (which IMHO is a sort of admission of the ML/functional family superiority).
Once you embrace the power of pattern matching, it's hard to use anything but an ML family language (StandardML/Haskell/F#/OCaml). I would program in any one of those languages over Java/C++/Objective-C/JS.
Practical reasons why you might choose OCaml:
- OCaml's records aren't as elegant as SML's but OCaml has labeled arguments with optional default values which can satisfy many of the reasons why you'd use records as arguments in the first place (and may be even more powerful in some cases).
- Two modes of compilation (fast native executable XOR fast compilation). Who doesn't like options.
- All the benchmarks I can find show that OCaml is very fast (around as fast as C++).
- Excellent JS target and and apparent commitment to maintaining it (as someone building a JS library, this is very important to me) (and as someone who wants to build apps and be able to instantly share them with everyone in the world.)
- Someone has built an autocomplete plugin for Vim/Emacs (merlin). ("VimBox" (https://github.com/jordwalke/VimBox/ https://github.com/jordwalke/VimBox/) has configured it to complete as you type - like in Visual Studio etc.)
- On very rare occasion, you'll run into a problem that is inherently better suited to OO (dynamic dispatch). I can usually find a way to solve it with functors/modules, but it's nice to know that you have OO in your back pocket in case you ever need it. It's also nice to know you probably won't have to.
- Finally, a common package manager (OPAM) is becoming standard. I look forward to seeing how OPAM helps make the new dev experience and the code-sharing/development experience seamless.
- The module system is very powerful (SML's). Haskell does not have this, and strangely F# dropped it. (I hear, Haskell's type classes fulfill similar roles (but with more sugar)).
- There's usually ocamlyacc grammars for most languages. Most examples of languages, type systems, parsers are already in OCaml (or ML). It's a nice (but small) perk.
- Predictability. OCaml is not lazy by default. Lazy computations could become problematic for low-latency applications (such as UIs) if a lot of computation becomes is deferred until the moment a final dependency has been satisfied, but by that time you may be close to your screen refresh deadline and it may lead to a dropped frame. It would have been better to have been computing while waiting for a final dependency. I'm not sure if Haskell (a lazy language) has had this problem. You can opt into laziness in OCaml if you would like to.
- Mutability. I feel strange saying this, as such a huge proponent of immutability, but sometimes you just need to hack something in place, mutate some state and come back to clean it up later. (You can still use monads in OCaml).
- Tagged Variants (no need to predeclare variants, just pattern match on them and OCaml ensures that only properly matched values ever make their way into that expression).
- Industry use is growing. OCaml is used here at Facebook and many other places as mentioned.
- There are many abstractions to choose from (Records, Objects, Modules, Functors, First Class Modules, GADTs, ...).
OCaml Cons:
- There are many abstractions to choose from (Records, Objects, Modules, Functors, First Class Modules, GADTs, ...).
(Edited for formatting)
- mercurial 12y ago> Java/C++/ and many other languages are just now beginning their slow, but inevitable evolution into becoming a dialect of ML (which IMHO is a sort of admission of the ML/functional family superiority). You can't be serious. The fact that they are borrowing some features from functional languages is in no way an indication that they're going to turn into an ML dialect. They will remain imperative languages, with all that entails (mutability, inheritance, etc) for the foreseeable future. Hell, even Scala isn't an "ML dialect".
- tomp 12y agoFor me, ML is not characterized by immutability and lack of inheritance, but rather by: first-class functions (C++, Java, Scala), type inference (partially in C++, C#, Scala), and algebraic datatypes and pattern matching (only Scala so far). The rest is just noise/sugar. So, by that measure, most languages are on their way to becoming ML.
- jordwalke 12y agoIt's a bit of hyperbole, sure, but I think there's some truth in it. For many of these, the progress is pretty slow - starting with simple type inference, lambdas. OCaml has imperative/OO features but it is still an ML language. As other languages adopt many of the ML features they will retain their roots of course. But I'd like to think they'll accumulate all of the good parts of an ML language (pattern matching, better inference). It's possible that my belief that ML languages are far superior to popular languages could be influencing my outlook. But even after many years of programming in terrible languages like Java it's clear within a week of programming in OCaml/ML that so many problems that require "architecting" in Java/ObjC/C++ are just trivial chains of functions and pattern matching in OCaml. How could the FP paradigm (ML in particular) not prevail? I can't imagine a world where it doesn't.
- mercurial 12y agoBut none of these languages will ever adopt Hindly-Milner type inference, which is the staple of the ML family. I mean, you have fist class functions and pattern matching in Erlang, and a library for pattern matching in Clojure, but this doesn't make them members of the ML family. And I really don't see Java and friends evolving much beyond that.