Version française
Home     About     Download     Resources     Contact us    
Browse thread
different records, same field name?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Kuba Ober <ober.14@o...>
Subject: Re: [Caml-list] different records, same field name?
On Monday 03 March 2008, Basile STARYNKEVITCH wrote:
> mfmorss@aep.com wrote:
> > In any case, is it possible to define and use different types of records,
> > which nevertheless share some field names?  I have had difficulty doing
> > this in the toplevel.
>
> It is not possible, for a simple reason:
>
> Suppose you have two record type tr1 & tr2 sharing a common field name f
>
>    type tr1 = { f : int; f1 : string }
>
>    type tr2 = { f2 : float;  f: int }
>
> Then the compiler won't be able to infer the type of function ff
> retrieving this field f
>
>    let ff x = x.f
>
> So the requirement that each field name is different is the price to pay
> to have type inference.

I think that type inference in OCaml sometimes gets in the way of other 
language features. I'd much rather see OCaml go in a direction where type 
inference is performed on "as far as it can go" basis.

In the cited example, if the compiler couldn't infer the type, it should 
simply remind the user to use explicit type as appropriate in such case.

While I do admit that this may cause some "silly" behavior, where changes in 
unrelated code can cause compile errors elsewhere, this should be naturally 
shielded by module boundaries. So, as far as I can see, it wouldn't suddenly 
break precompiled modules, at least.

Cheers, Kuba