3 ms·
I think OCaml does what you are looking for. See the example on https://en.wikipedia.org/wiki/Structural_type_system https://en.wikipedia.org/wiki/Structural_ty
by default-kramer 9y ago
I think OCaml does what you are looking for. See the example on https://en.wikipedia.org/wiki/Structural_type_system https://en.wikipedia.org/wiki/Structural_type_system, which features OCaml.
- timthelion 9y agoThanks, looks interesting. One of the things I've always wondered about with this approach is, how do you then show the "duck type"s in a succinct and human readable way. This is pretty succinct, but I'd have to say that it is not obvious what the syntax means: val y : < get_x : int; set_x : int -> unit > = <obj> Perhaps the example was poorly chosen, what is "unit"?
- deciduously 9y agoIt's () in haskell. Here's a good write-up: https://stackoverflow.com/a/16893900 https://stackoverflow.com/a/16893900
- wtetzner 9y agoUnit is a type that has exactly one value, (). It's often used like `void` is used in C-like languages. So this: val y : < get_x : int; set_x : int -> unit > means an object that has a get_x method that returns an int, and a set_x method that takes an int and returns unit (void).