|
|
|
|
|
by hajile
2509 days ago
|
|
> OCaml structs are structurally typed. You can read about Ocaml's nominal record typing in the [ReasonML docs](https://reasonml.github.io/docs/en/record) (it's more clear there IMO). SML gets this right in my opinion. If I create a record `{foo = "abc", bar = 123}`, I can pass that record on to ANY function that needs a record that looks like {foo:string, bar:int} fields because it looks at the structure rather than the type of the record constructor. Another nice property of the SML approach is that you don't need named arguments. Instead, pass a tuple with names (aka a record). One set of syntax rules covers both cases. I also rather like the ability to access record fields with the hash syntax (eg, `#foo myrecord`) when I don't want to destructure. |
|
You are confusing records with structures, it seems. Structures exist in module language. Records are nominal in SML as well. Access functions for tuples and records are a dirty ugly hack build-in for convenience, they have nothing to do with structural typing, they are inferred in place.
OCaml has structural typed records, they are called objects.
is structurally typed record.> I can pass that record on to ANY function
No, you can't.
works, but doesn't. It's an ugly hack, typing is still nominal. A hack, just like SML's arithmetic op overload.Compare it to OCaml, which have proper structural typing for objects and raw polymorphism
Edit: sorry, indeed typing is structural since you can write `{x:typ} -> typ`.Anyways OCaml have structural typed records and raw polymorphism and subtyping support for them.
> Another nice property of the SML approach is that you don't need named arguments. Instead, pass a tuple with names (aka a record).
What's your point? You can use records as arguments in OCaml as well.