At what point is the language going to get serious adoption? It's mature, unbloated, elegant, and fun to write. The only thing I really think needs improvement is its multicore stuff.
The problem is that pretty much the entire standard library isn't thread safe. It would take some serious work to get OCaml ready, unless you stick to message passing between distinct processes.
I don't think this is correct. All pure data structures are threadsafe. It's true that mutable datastructures like the built-in hashtable are not threadsafe. But isn't that the standard tradeoff with threads? Most things aren't threadsafe, but with mutexes and condition variables and the like, you can make them so. Surely one doesn't want to make the performance destroying mistake of making _everything_ threadsafe.
To be clear, it is true that the runtime has a single lock for the GC, so for now, it's not possible to gain physical parallelism from multiple threads. That's being worked on, but I have to say that I rather prefer message passing and I don't know that how we build programs will change much when shared memory threads are available.
For example you can use them to write HTTP services/clients that scale to lots of concurrent connections without blocking, and the code you write is not that much different
from the one you'd write in direct-style.
Slight off-topic: Judging from H1B stats [0], Jane Street pays quite below the market rate (for NYC finance sector). Also, they haven't sponsored any H1B's for the last 3-4 years. I wander if these stats are reliable... Or maybe, JS puts more emphasis into bonuses (on top of the base salary), in order to not to disclose salaries?
Jane Street has offices in London and Hong Kong but their HQ is in NYC.
You don't need an H1B if you can recruit to a local office and then do an internal transfer (after some time). No idea if Jane Street does this but I know other tech companies do.
Not much need to make internal transfers - London and Hong Kong are two of world's largest financial centres so there's plently of local work to do and it's not exactly hard to collaborate with colleages in NYC from London when there's a good 4/5 hours a day of overlap when both offices are active.
Looking at salaries is misleading anyway - companies like Jane Street do most of their compensation as bonuses rather than salaries. Having worked in the proprietry trading world, salaries are generally on the low side, but your bonus can be anything from 50%-100% of your salary upwards. It differs a fair bit depending on what you do - a traders' bonus will be more volatile and pegged to their performance that year, a back office dev will propably have a more stable bonus year to year, but not as large.
I've been a Haskell user for about a year now, and am pretty comfortable with the language. Ocaml has a lot of things that I like (the module system seems great; strict evaluation is a win; mutability/loops/etc without having to jump through hoops). But every time I look at the actual code, I'm really turned off by a few things:
* lets everywhere, even at top-level. Haskell doesn't do this, which is a lot nicer.
* let rec. Wut?
* Apparently to produce code to run at top-level main loop you just assign it to a let _ = ..., which looks really hacky.
* Double semicolons.
* Type variables annotated with 'a. In Haskell, lowercase type names are variables, uppercase are constants (or type classes, but those aren't mixed).
* Mix of semicolons and end.
Haskell seems syntactically more elegant, has type classes which are (at least on the surface) more straightforward than modules, though I don't know which is more useful at the end of the day. Haskell's do syntax is very nice.
That said, in many ways OCaml seems much more like "my kind of language". But there are a number of turnoffs.
Hint: double semicolons are not necessary in files (just in the REPL). Also, personally I dislike Haskell's lower/uppercase conventions, and find OCaml's syntax marginally better in this respect (although I think best would be pre-declared parameters, like in C/Java/Scala/Julia).
I'd like to like ocaml but its not clear to me why I would choose it above anything else to write an application. I have the sense vaguely it might be a good thing. A little reading and examination of some example source hasn't thrown much light.
What's it good for? Why should I use it? Why is it a better choice than language X or Y?
"Among this worthy set of languages, OCaml stands apart because it manages to provide a great deal of power while remaining highly pragmatic. The compiler has a straightforward compilation strategy that produces performant code without requiring heavy optimization and without the complexities of dynamic just-in-time (JIT) compilation. This, along with OCaml's strict evaluation model, makes runtime behavior easy to predict. The garbage collector is incremental, letting you avoid large garbage collection (GC)-related pauses, and precise, meaning it will collect all unreferenced data (unlike many reference-counting collectors), and the runtime is simple and highly portable."
It's really fast, has minimal boiler plate and supports functional programming without any of the "orthodoxy" of Haskell. That is, you can write a recursive function but also write a for loop.
Imagine something that can compete with C and C++, but doesn't require all of the low-level reinvention and memory management. It's like a high-level language for smart people that doesn't feel entirely impractical. It has some things that people bitch about (like having to use +. to add two floats and + to add two ints), which don't really bother me that much.
If it were more popular and had better libraries/platform support (unless that's changed drastically in the past year or so), it would be a serious contender for general development. Being completely honest, I think Jane Street is probably the biggest organization pulling OCaml, and from a navel-gazing standpoint you can either view that as good or bad.
Use it because you learned it and thought it didn't suck, I guess?
> "... unless that's changed drastically in the past year or so ..."
Actually a lot has happened in the last year or so. If you're not aware of it, you could start by looking over the end-of-year post from OCaml Labs [1]. I'm aware that it's difficult to keep a central overview of all the progress but there is a lot of work being done, in many ways, to improve the ecosystem.
Whoa, that's cool. I had no idea people were still working on it at that level. The last thing I saw was a version of OPAM that didn't seem to work that well...I'll have to go look around.
I think we're pretty clearly the most intense user (most code, most developers using it), but not technically the biggest company using it. Bloomberg and Facebook are examples of bigger companies using it in serious ways.
We use it in our backend to tie everything together. It serves an http/json api to our client apps (Web/JavaScript, Android/Java; iOS/Objective-C coming soon). We're handling the same data types in JavaScript, Java and OCaml.
What is clear is that we spend a lot of time fixing type errors in JavaScript due to the lack of basic type checking. Java does not have this problem and for Android programming it is a breeze since really the mobile app is just a GUI which doesn't have to do anything clever with the data.
Now the backend is in charge of serving correct data to the client apps. I could explain that OCaml is fast and lets us implement any CPU-intensive algorithm without having to switch to C, but the truth is most of our code is not much about algorithms but very much about data modeling and data management. Anything we store needs to be stored correctly if we don't want to accumulate broken data resulting in bugs that are costly to fix. Untyped languages such as JavaScript let you store anything without checking their conformance to a type definition, this is awful. Java for example would be perfectly acceptable if it wasn't so verbose. We want to write JSON-like records and arrays like in JSON or JavaScript. OCaml provides this, Java doesn't.
Now, we don't want half of our code to consist in annotating each variable with a type. The compiler should infer that 123 is an int. How hard is that? You tell me. JavaScript doesn't check anything. Java or C++ force you to declare that 123 is an int. How clever is that? I don't know. OCaml, Haskell just figure out that 123 is an int. That's called type inference. That makes code readable like JavaScript or Python, with the safety of Java and the performance of C++.
It's not clear to me why you'd choose OCaml over Haskell. I understand that OCaml isn't lazily evaluated like Haskell and has a simpler to understand compiler which gives a better understanding how the code will perform at runtime. But why as someone new why would I choose OCaml over Haskell?
From my perspective it seems like there is more activity and research going on in Haskell.
> I understand that OCaml isn't lazily evaluated like Haskell and has a simpler to understand compiler which gives a better understanding how the code will perform at runtime. But why as someone new why would I choose OCaml over Haskell?
Haskell comes with a very big community, and a lot of ways to start with the languages. I think about the http://learnyouahaskell.com/ and others.
Real World OCaml is that, without totally being it. The authors wrote it with the idea in mind that only people with a good background about functionnal world can read it.
I'd add that lazyness can be a good thing and that the type-system of Haskell is better than the OCaml one.
As I'm aware, finance industry now prefers F#. Regarding Haskell - one of its biggest user (Standard Chartered I think) uses in-house dialect of it, miu, which is strict. Basically, it just syntactically resembles Haskell... i.e. you can't just use some random Haskell library with it.
Where did you hear about this? Googling around for "miu", "miu haskell", "miu haskell standard chartered", "miu haskell standard chartered strict", etc hasn't given me anything.
For our web app we've been sticking with JavaScript so far. It's a conservative choice with a lot of problems due primarily to the absence of typing. The absence of typechecking is a problem for our moderately complex app (> 10K lines).
Alternatives that we've been considering include js_of_ocaml and TypeScript.
For the kind of Android app we're developing we wanted a native look-and-feel. That means using the Android SDK, and I don't know how going through OCaml would make that any easier.
apart from the more objective criteria, it's simply a very pleasant language to use. i usually evaluate new application programming languages by what they offer me that ocaml does not.
Algebraic data types and pattern matching are bliss. It's almost a cliche, but you don't know what you're missing without them and once you know them you'll wish every other language had them.
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/) 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, ...).
Quite a good summary. I use OCaml at Red Hat for a lot of virtualization tools. I had reason to use Python for some code quite recently, and it reminded me of how much better ML-derived languages are.
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.
> 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".
It'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.
But 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.
Honest question: Why would/could they not? There has been a ton of research published on extending HM type inference to support structural subtyping, row polymorphism, extensible records which very closely model many (but not all) of the concepts in Object Oriented systems.
That's actually a good question. I suppose that it would be possible in theory to try and reconcile the two, but I don't see that happening. And I don't know how much you can extend HM to support the full C++ featureset. I'd be surprised if it was possible.
I haven't seen the entirety of C++'s feature set added to an HM system, but even OCaml has an OO system that's fairly complete. Many new functional languages are adopting Daan Leijen's extensible records approach which can model OO fairly well. http://research.microsoft.com/pubs/65409/scopedlabels.pdf
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.
For 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.
I'll just add in the Cons: no ad-hoc polymorphism. This is what causes ocaml to use different operators for each type (+, +., +|, etc), which is what most beginners don't like about the language.
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 honestly haven't been bothered by the lack of ad-hoc polymorphic.
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).
http://www.reddit.com/r/programming/comments/25vr5t/ocaml_40...