Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: John Gerard Malecki <johnm@artisan.com>
Cc: OCAML <caml-list@inria.fr>
Subject: Re: [newbie] Define and use records in sum types
Date: Wed, 19 Jul 2000 22:10:48 +0200	[thread overview]
Message-ID: <20000719221048.B23676@miss.wu-wien.ac.at> (raw)
In-Reply-To: <14709.63462.792269.194367@ish.artisan.com>; from johnm@artisan.com on Wed, Jul 19, 2000 at 11:48:06 -0700

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



  parent reply	other threads:[~2000-07-21  7:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-11 11:32 David Mentré
2000-07-17 10:01 ` Markus Mottl
     [not found]   ` <14709.63462.792269.194367@ish.artisan.com>
2000-07-19 20:10     ` Markus Mottl [this message]
2000-07-21 12:23       ` Frank Atanassow
2000-07-21 20:00         ` Markus Mottl
2000-07-22 13:34           ` Frank Atanassow
2000-07-22 18:31             ` Markus Mottl
2000-07-23 13:55               ` Frank Atanassow
2000-07-23 15:20                 ` Markus Mottl
2000-07-24  9:28                   ` Frank Atanassow
2000-07-25  5:26                     ` Alan Schmitt
2000-07-19 19:21 ` Jean-Christophe Filliatre
2000-07-20  7:08   ` David Mentré

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20000719221048.B23676@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=caml-list@inria.fr \
    --cc=johnm@artisan.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox