Browse thread
[newbie] Define and use records in sum types
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Markus Mottl <mottl@m...> |
| Subject: | Re: [newbie] Define and use records in sum types |
On Wed, 19 Jul 2000, John Gerard Malecki wrote:
> Many of the professional programmers I show ocaml to like it very much
> but are annoyed by this behavior. Maybe there is a reasonable work
> around? The normal solution for the behavior of ocaml records is to
> prepend the record name to the field name. For example,
>
> type male = { male_name : string; male_salary : int }
> type female = { female_name : string; female_salary : float }
Ahem, do you want to express with this example that women earn so little in
your company as compared to men that you need floats to express their
salary? ;-) (only joking!)
Well, anyway, modules solve the problem quite well - why not use them? The
effort is hardly worth mentioning.
E.g.:
module Male = struct
type t = { name : string; salary : int }
let raise_salary p n = { p with salary = p.salary + n }
end
module Female = struct
type t = { name : string; salary : float }
let raise_salary p n = { p with salary = p.salary +. n }
end
let _ =
let male = { Male.name = "Albert"; Male.salary = 1000 }
and female = { Female.name = "Berta"; Female.salary = 1000.0 } in
let happy_male = Male.raise_salary male 100
and happy_female = Female.raise_salary female 100.0
and other_female = { female with Female.name = "Caroline" } in
()
> but then reference
>
> { male:salary = x }
The module version nearly looks the same ("." instead of ":" and capital
letter). Additionally, it forces people to introduce some modularity into
the program, which is hardly a drawback.
> The idea is to allow the disambiguation to be optional. It may
> confuse novices but ...
I'd opt against this: to me, modules already provide a regular way to
organize namespaces - no need to add even further constructs to the
language...
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl