Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Label Names Space - philosophy or implementation?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <weis@p...>
Subject: Re: Label Names Space - philosophy or implementation?

> A co-worker has (vehemently) pointed out to me that record label names
> cannot be shared.  For example, the following fails
> 	type foo = { name : string; x : int }
> 	let    a = { name = "foo";  x = 1 }
> 
> 	type bar = { name : string; y : float }
> 	let    b = { name = "bar";  y = 9. }
> 
> 	let   a' = { name = "foo";  x = 2 }
> With an error message on the last line that "The label x belongs to
> the type foo but is here mixed with labels of type bar".  This annoys
> my co-worker to no end.  He would like many of his records to have a
> field with a standardized label.  This makes it easier for him to
> write printers for his data-structure.  Does anyone have a
> recommendation to make to him?

This feature is used for sake of simplicity in the type-checking process.
We used to have label overloading in records, but it was a bit
complex to implement, since you can guess no more the type of a label
from its name. In the worst case (polymorphic labels), the
type-checking of labels is exponential (I mean, noticeably exponential
in practice, instead of our good old apparently quasi-linear regular
algorithm). Furthermore, people do not use it that much.
Its a common habit to prefix the labels with the name of the type:
something like:

 	type foo = { foo_name : string; foo_x : int }
 	type bar = { bar_name : string; bar_y : float }

> Until this morning I would have guessed that the type of a record was
> not resolved until all of the labels were examined.  As I re-read the
> caml-light and ocaml manuals I see that this is not true.
>
> Are there good reasons for this "restriction"?  I can see how this
> this might be considered a good software practice.  Maybe, due to
> pattern matching, it is not possible to have shared labels.

It is definitively possible to have what you call ``shared'' labels,
that is overloaded labels. It is just a bit complex to add to the
type-checker.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis