Version française
Home     About     Download     Resources     Contact us    
Browse thread
Record field label locality
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Record field label locality
Brighten Godfrey wrote:
> Actually, what I want seems to be the way OCaml treats methods in
> objects: given an object, you can name the method directly without
> mentioning its module.  I can write a function
> 
>     let f x = x#some_method "argument"
> 
> where `x' might be an object defined in another module, or locally.  Why
> can't records be handled like this?
> 

1) Implementation
Record field access is almost identical to array lookup -- internally,
records are stored as arrays and during compilation the field name gets
translated to the correct index to get.  But since type information goes
away after compilation (including record field names), there's no way to
do the same kind of dispatch you get with objects.

Then you run into the problem that the record field labels aren't
global, so you could have the same label as different indexes in
different modules.  Thus the compiler needs to know which module that
record field came from to do the conversion to field index.

2) Typing of x.field

Given the following:

module M1 = struct type t = { f1 : int } end
module M2 = struct type t2 = { f2 : int; f1: string } end

let get_f1 x = x.f1

How should f1 be typed?   M1.t -> int  or M2.t2 -> string?  And how to
deal with separate compilation, such that M1 and M2 aren't even in the
same file as get_f1?

E.