7 ms·
Here's a copy of a previous post I've made on HN about why you might want to choose OCaml instead of another language. Nothing has changed about my opinion sinc
by jordwalke 12y ago
Here's a copy of a previous post I've made on HN about why you might want to choose OCaml instead of another language. Nothing has changed about my opinion since the time I wrote it:
Original link: https://news.ycombinator.com/item?id=7766315 https://news.ycombinator.com/item?id=7766315
------
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)
- jordwalke 12y agoTwo weeks after the original time of writing, Apple released Swift - which is considerably influenced by the ML family of languages.
- deleted 12y ago[deleted]
- amirmc 12y agoIndeed. I heard someone comment that every FP conference will be doing a 'victory lap' due to Apple's announcement. At the very least, I hope people/devs will explore ML based languages for their next project. It would be interesting to get your take on the toolstack we're building using OCaml - http://nymote.org http://nymote.org (I'm aware the copy on the site needs works but it's the tools I'm referring to).
- AndreasFrom 12y agoBryan O'Sullivan commented that https://mobile.twitter.com/bos31337/status/473569531366211584 https://mobile.twitter.com/bos31337/status/47356953136621158... and Graydon Hoare referenced it in his post on Swift http://graydon2.dreamwidth.org/5785.html http://graydon2.dreamwidth.org/5785.html
- amirmc 12y agoYup, that's what I heard (obviously 3rd hand). Second like is very useful as I've not had a chance to look at the swift docs yet.
- jordwalke 12y agoEach of the components of nymote are interesting in their own right. I'm very interested to see how they can all be brought together to accomplish your goals which are ambitious and noble. How can I keep up to date on your progress?
- Paradigma11 12y ago"- 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))." F# runs on the standard .net type system which is not able to do type classes, modules or higher kinded generics. On the other hand you have compatibility to all .net libraries which is a huge plus.