0

I'm looking at GADT definitions, and they use objects instead of records. I was wondering why, as it seems like objects are pretty much never used in general in OCaml. Is there a difference?

For example:

type (_, _, _) Basic.t +=
  | Field : ('field1, 'field2, < field1 : 'field1 ; field2 : 'field2 ; .. >) t
David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
  • Objects are structural and subtype polymorphic, which means one object type can be compatible with another if it includes the same structural elements. Note the `..` at the end of the type here, which means it is "open" and will be compatible with types that have at least these fields. Beyond that I can only speculate. – glennsl Apr 22 '22 at 13:06
  • @glennsl, OCaml doesn't have subtype polymorphism, only row polymorphism. That is why we need sometimes to explicitly upcast to the base type using `:>`. – ivg Apr 22 '22 at 13:47

1 Answers1

0

Objects offer a unique type system feature, called row polymorphism. It is hard to tell without any more context why is row polymorphism required in this particular example, I can only tell that this constructor represents a family of row polymorphic types of objects that has at least two methods, field1 and field2 of type 'field1 and 'field2 correspondingly.

ivg
  • 34,431
  • 2
  • 35
  • 63