The things I found awful about Python compared to OCaml:
- Lots of bugs which are never noticed, even at run time, eg. along error paths. 'pylint' helps here, but it doesn't catch all the bugs, and IMHO if you need a lint tool to fix your language you're doing it wrong in the first place.
- Really hard to refactor code. In OCaml the compiler helps refactoring -- you just break something and hit 'make -k' until you've fixed all the places that need fixing. In Python you have to visually grep all the code, which doesn't scale and is bug-prone.
- I miss nested variable declarations.
- Poor support for data types. Python in theory has lots, but in practice everyone's using dictionaries and objects, which are unsafe and introduce bugs by design.
- Slow. Really slow.
There were a few good aspects of Python, but I ended up with a lot of buggy code, and I know there are many more bugs in there which I haven't yet found, which just wouldn't be the case with a statically typed, type-inferenced ML-derived language.
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".
All of these are extensions of ML that are shipped into production today, so it looks like more than just a theory at this point.
Delimited overloading[1] is a nice progress on this front that I wished I knew when I started using ocaml. Though I admit you can later appreciate the benefit of not having ad-hoc polymorphism in several situations.
I'll add to the cons though just for completeness.
- The standard lightweight way of achieving higher ranked polymorphism is through record types, which is strange.
type wrapper = {call: 'a. 'a->'a}
let myFun x = (x.call "asdf", x.call 10)
As far as I know (someone correct me) both Haskell and OCaml require that you do something special to indicate this level of polymorphism, so that in itself is not a "Con" for OCaml, but there could have been a way to annotate the argument to the function itself as a polymorphic function without needing the intermediate record wrapper type (I believe Haskell has this feature). A benefit of the OCaml approach is that you get to pretend that HM inference is powerful enough to infer that degree of polymorphism and not annotate your function arguments (because record types must be predeclared) and it recognizes the type of record based on the `.call` access.- The syntax is kind of strange some times. Some of this is because they've eliminated the requirement that every `let` have an `end`. That does end up making other things more pleasing to read:
let x = y in
let p = z in
expresion
- There are three ways to declare functions.Syntactic sugar for curried function. Each argument allows one pattern matching case inline.
let f x y =..
let f (x1, x2) (y1, y2) = x1+x2+y1+y2
Same as syntactic sugar above but anonymous. Still only one pattern match per argument. fun x y -> ..
fun (x1, x2) (y1, y2) -> ..
Anonymous, non-curried function (which only allows a single argument) but allows multiple pattern match cases on that single argument. function Some x -> "1" | None -> "0"
So there's some intricacies to the syntax, but these are pretty mild annoyances and they have other benefits that aren't immediately apparent. Something as trivial as syntax shouldn't be something that you base your language decision on, especially since OCaml has always allowed highly configurable (almost arbitrarily configurable) syntax via CamlP4 (and now even simpler extension points).
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/) 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)