OCaml’s polymorphic comparison operators are a useful but highly problematic feature of the language. I wrote a blog post about this some years ago, and the basic gist of it is: OCaml’s polymorphic comparison functions are lightweight and convenient, but have occasionally crazy semantics which make them error prone. To add insult to injury, the performance is also not great.
For this reason, we decided that we wanted to remove polymorphic comparison from the namespaced exposed by
Base from the very beginning. As a result, once you open
Base, you’ll discover that operators like
= only work on integers, and that you’ll need to use type-specific comparison functions when dealing with other types. You can do this manually, i.e.:
open Base let b = List.equal Int.equal [1;2;3] [4;5;6]
Or you can do it using a syntax extension:
let b = [%compare.equal: int list] [1;2;3] [4;5;6]
Note that polymorphic comparison is still available, and can brought back in scope by opening
I’m writing this email because we are planning on doing the same thing to
Core_kernel, to make them consistent with
Base. Given that this is a serious breaking change, we wanted to give some advance warning and get some feedback.
Note that this is a relatively easy change to adopt. Internally, we’re going to mechanically replace
open Core with
open Core open Core.Poly, and similarly with
Core_kernel. It’s a fairly simple textual change, so it’s not clear that an upgrade tool is worthwhile, but that is something worth discussing.
Anyway, feedback on all of this is welcome.