Why doesn't OCaml support record subtyping?
Asked Answered
E

2

15

Reading "Types and programming languages", I got impressed by the object implementation using closures and record subtyping (chapter 18). Is there any particular reason OCaml doesn't support record subtyping (I know objects do, though)? In fact, I can't find any language that support this.

Embarrassment answered 6/3, 2013 at 1:56 Comment(2)
SML# has record subtyping. OCaml's objects are records with subtyping. Structural subtyping is powerful, but its type errors are often cryptic. So OCaml users normally prefer simpler records without subtyping.Burka
Thanks for pointing out SML#! Is there any difference between the record subtyping in SML# and object structural typing in OCaml?Allure
S
23

Technically, OCaml's objects do not really support subtyping in the usual sense, but row polymorphism. There are many advantages of row polymorphism over subtyping, in particular it both is more expressive and works much better with type inference (subtyping and type inference do not mix well at all).

The primary problem with using either structural subtyping or row polymorphism for all records is that it requires a significantly more involved runtime implementation, and consequently, also is more expensive. Where simple records can trivially be translated into plain tuples, with field access being just indexing, structural subtyping or row polymorphism require the ability to transparently "slice" an object, i.e. view it under a supertype with random fields removed. In general, this requires either field lookup by hashing (such as Ocaml's objects), or evidence passing techniques, where the index of every field used by a function or any of its callees has to be passed as a hidden argument in addition to the actual record (that's what SML# is doing, for example).

In any case, Ocaml has polymorphic records, they are just called objects. But you can ignore all the class mess around them if you don't need it.

Stutzman answered 6/3, 2013 at 7:11 Comment(1)
Yes, the class mess was what put me off. I'm new to OCaml, but I'm looking forward to learn more about its object system. Anyway, thanks for you answer!Allure
O
9

Record subtyping is quite dangerous on a typing point of view.

Let's say you have a record with three fields: a, b and c. And you want to create a record with only two fields: a and c. The compiler doesn't know what type you're using until it finishes reading the whole record, and still at the end, cannot help you if you made a mistake : you forgot the b field for example.

I fully agree that this point of view is contestable, but I think that's how people who write ocaml think.

Orthodontia answered 6/3, 2013 at 7:3 Comment(2)
Thank you for pointing this out. I would upvote, but my reputation is too low ;)Allure
Hmm, I don't quite understand. Subtyping means you can use a 3-field record in places that expect a 2-field record. But in your example, you create a 2-field record (possibly by mistake). Even with subtyping, the compiler would allow you to use it only in places that actually expect 2 fields (or less). But that's fine, since those places will never even look at your 3rd field.Straightlaced

© 2022 - 2024 — McMap. All rights reserved.