5 ms·
The HM type inference used by OCaml breaks down when you add in nominal subtyping, which is common in mainstream static languages like Java. OCaml handles this
by iaabtpbtpnn 6y ago
The HM type inference used by OCaml breaks down when you add in nominal subtyping, which is common in mainstream static languages like Java. OCaml handles this by giving up subtyping; there is none in the core language, and the (rarely-used) object system uses a form of structural subtyping based on row types.
- ohadrau 6y agoWhile the object system is pretty rarely used [1], polymorphic variants see quite a bit of use and are also based on row types. [1] Recently have been seeing a lot more use in BuckleScript because of the JS object representation
- xvilka 6y agoI once wondered about subtyping[1] too. Looks like there's a possible solution - MLsub[2][3]. But it will require significant amount of work to modify OCaml inference engine to work that way. For now I just use functors for types with the shared fields. It doesn't add too much lines of code either. [1] https://discuss.ocaml.org/t/function-argument-polymorphism/3224/2 https://discuss.ocaml.org/t/function-argument-polymorphism/3... [2] https://www.cl.cam.ac.uk/~sd601/papers/mlsub-preprint.pdf https://www.cl.cam.ac.uk/~sd601/papers/mlsub-preprint.pdf [3] https://github.com/stedolan/mlsub https://github.com/stedolan/mlsub
- yawaramin 6y agoOCaml also has a really good subtyping implementation in the often-used polymorphic variant system. I'll just quote from http://roscidus.com/blog/blog/2014/02/13/ocaml-what-you-gain/#polymorphic-variants http://roscidus.com/blog/blog/2014/02/13/ocaml-what-you-gain... : > Their real purpose is to support subsets and supersets, which are useful all over the place. And indeed they are super useful, e.g. I am using them to implement functions that deal with web responses: some can deal with only HTTP responses, some with only WebSocket responses, and some with either. All completely type-safe and extensible.