From: Brighten Godfrey <pbg@cs.berkeley.edu>
To: Edgar Friendly <thelema314@gmail.com>
Cc: Jon Harrop <jon@ffconsultancy.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Record field label locality
Date: Tue, 12 Aug 2008 18:17:22 -0700 [thread overview]
Message-ID: <735BE8E5-2CCB-41FA-87E7-8E4027966048@cs.berkeley.edu> (raw)
In-Reply-To: <48A226E3.5050500@gmail.com>
On Aug 12, 2008, at 5:12 PM, Edgar Friendly wrote:
> 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.
Thanks, that helps. But what about handling typing as with objects,
but without the dynamic dispatch that you get with objects? (see below)
> 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?
Two things come to mind:
(1) The type of get_f1 is handled analogously to the way it is
handled for objects, something like this:
val get_f1 : < x : 'a; .. > -> 'a = <fun>
I'm guessing that if you did this, you would have to "instantiate"
`get_f1' each time it is applied to a new record type, which I assume
is inconvenient (or not?).
(2) Require that all record field accesses refer to a globally-unique
record type, making conversion to a record field index is easy. So
the example code Edgar gave would result in a compilation error
because the compiler cannot determine which `.f1' field the access
refers to. But consider this code:
let return_garlic () =
let x = {M2.f2=5; M2.f1="garlic"} in
x.f1
In line 2, globally unique record field names are given, which allows
the compiler to tag variable `x' with type `M2.t2'. Then in line 3,
the record field access `x.f1' can only mean `x.M2.f1'.
Summary: I can see why it is useful to require that each field
access be mapped to a globally-unique record type. OCaml today does
this by having the programmer explicitly name a globally-unique
record type with every field access. But couldn't this instead be
done by type inference?
Thanks very much for your reply.
~Brighten
next prev parent reply other threads:[~2008-08-13 1:17 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-10 10:04 Brighten Godfrey
2008-08-10 19:38 ` [Caml-list] " Jon Harrop
2008-08-12 21:03 ` Brighten Godfrey
2008-08-13 0:12 ` Edgar Friendly
2008-08-13 1:17 ` Brighten Godfrey [this message]
2008-08-13 12:48 ` Edgar Friendly
2008-08-14 6:38 ` Brighten Godfrey
2008-08-14 10:11 ` David Allsopp
2008-08-13 1:51 ` blue storm
2008-08-13 8:14 ` Richard Jones
2008-08-13 9:30 ` Brighten Godfrey
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=735BE8E5-2CCB-41FA-87E7-8E4027966048@cs.berkeley.edu \
--to=pbg@cs.berkeley.edu \
--cc=caml-list@yquem.inria.fr \
--cc=jon@ffconsultancy.com \
--cc=thelema314@gmail.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