From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA19842 for caml-red; Fri, 21 Jul 2000 09:25:32 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA19938 for ; Wed, 19 Jul 2000 22:10:51 +0200 (MET DST) Received: from miss.wu-wien.ac.at (miss.wu-wien.ac.at [137.208.107.17]) by nez-perce.inria.fr (8.10.0/8.10.0) with ESMTP id e6JKAoP29467 for ; Wed, 19 Jul 2000 22:10:50 +0200 (MET DST) Received: (from mottl@localhost) by miss.wu-wien.ac.at (8.9.0/8.9.0) id WAA07766; Wed, 19 Jul 2000 22:10:48 +0200 (MET DST) Date: Wed, 19 Jul 2000 22:10:48 +0200 From: Markus Mottl To: John Gerard Malecki Cc: OCAML Subject: Re: [newbie] Define and use records in sum types Message-ID: <20000719221048.B23676@miss.wu-wien.ac.at> References: <20000717120151.A32148@miss.wu-wien.ac.at> <14709.63462.792269.194367@ish.artisan.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2i In-Reply-To: <14709.63462.792269.194367@ish.artisan.com>; from johnm@artisan.com on Wed, Jul 19, 2000 at 11:48:06 -0700 Sender: weis@pauillac.inria.fr 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