4 ms·
Structural 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;;
by lapinot 5y ago
Structural 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...