3 ms·
I think OCaml isn't just structural. For example, sum types (variants in OCaml) have a nominal form which is the default: https://ocaml.org/manual/coreexamples.
by Zababa 5y ago
I think OCaml isn't just structural. For example, sum types (variants in OCaml) have a nominal form which is the default: https://ocaml.org/manual/coreexamples.html#s%3Atut-recvariants https://ocaml.org/manual/coreexamples.html#s%3Atut-recvarian..., but also a structural form, polymorphic variants: https://ocaml.org/manual/polyvariant.html https://ocaml.org/manual/polyvariant.html. I think it's the same for records, I didn't find a way to express the type "a record that has a certain field" as an argument of a function. You can do this with objects and modules I think, but not with records.
- lapinot 5y agoStructural records are in fact "object types" in ocaml, which you can get the shell to tell you by calling a random method name. # let f x : int = x#foo;; val f : < foo : int; .. > -> int = <fun> Actually there is not really a type of "a record that has at least some fields": # type t = < foo : int; .. >;; Error: A type variable is unbound in this type declaration. In type < foo : int; .. > as 'a the variable 'a is unbound Its hidden (row) polymorphism, it's not subtyping but type abstraction. We're implicitely quantifying over the actual type: # type 'a t = < foo : int; .. > as 'a;; type 'a t = 'a constraint 'a = < foo : int; .. > For the record (ha!), for people who don't know ocaml here are "structural sum types", or polymorphic variants as we call them. # let x = `Bar;; val x : [> `Bar ] = `Bar above: type of `Bar is a sum containing "at least" (polymorphic) `Bar # type int_opt = [ `Some of int | `None ];; type int_opt = [ `None | `Some of int ] above: some "exact" nameless sum type, with arguments # type 'a t = [< `Foo | `Bar ] as 'a;; type 'a t = 'a constraint 'a = [< `Bar | `Foo ] above: polymorphic type of sums which contain at most these two constructors. refs: https://dev.realworldocaml.org/objects.html https://dev.realworldocaml.org/objects.html https://dev.realworldocaml.org/variants.html#polymorphic-variants https://dev.realworldocaml.org/variants.html#polymorphic-var...