|
|
|
|
|
by groovy2shoes
3489 days ago
|
|
Which dialects? Both Standard ML and OCaml can handle multiple record types with homonymous fields just fine: $ poly
Poly/ML 5.5.2 Release
> type point2 = {x : real, y : real};
type point2 = {x: real, y: real}
> type point3 = {x : real, y : real, z : real};
type point3 = {x: real, y: real, z: real}
> let val pt = {x = 1.0, y = 2.0} in #x pt end;
val it = 1.0: real
> let val pt = {x = 3.0, y = 4.0, z = 5.0} in #x pt end;
val it = 3.0: real
>
$ ocaml
OCaml version 4.03.0
# type point2 = {x : float; y : float};;
type point2 = { x : float; y : float; }
# type point3 = {x : float; y : float; z : float};;
type point3 = { x : float; y : float; z : float; }
# let pt = {x = 1.0; y = 2.0} in pt.x;;
- : float = 1.
# let pt = {x = 3.0; y = 4.0; z = 5.0} in pt.x;;
- : float = 3.
#
The only nuance with record labels in ML, as far as I can remember, is that the type of projection operations can be ambiguous if the exact type of the record is unknown (e.g., in `(fn pt => #x pt)`, the type of `pt` cannot be inferred since it could be `point2`, `point3`, `{x : int}`, `{x : real}`, ...; but this can be disambiguated with a type annotation: `(fn (pt : point3) => #x pt)`).Perhaps I've misunderstood the question. If so, can you give me an example? |
|
Anyway, good to know that it's definitely not OCaml!