3 ms·
What about Go and OCaml? And I'm sure there are others.
by dgb23 5y ago
What about Go and OCaml? And I'm sure there are others.
- sizediterable 5y agoI forgot about OCaml (since I normally associate it with the more sophisticated typing stuff). I don't know if I'd completely count Go since interfaces only apply to methods, not fields
- dgb23 5y agoDisclaimer: Not a regular Go user. I was thinking of how you can embed types in structs. You automatically dereference the fields and methods of the components directly from the outside.
- BobbyJo 5y agoYep, in go the inheritance (using the term loosely) structure can apply to interfaces (which specify groups of functions) and structs (which specify groups of data) and roughly does the same thing in both instances.
- Zababa 5y agoI 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...