English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] extensible records again
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-03-31 (10:05)
From: Marcin 'Qrczak' Kowalczyk <qrczak@k...>
Subject: Re: [Caml-list] extensible records again
W li¶cie z sob, 20-03-2004, godz. 22:21 -0800, Michael Vanier napisa³:

> I've written a dynamically-typed language in ocaml, and I want to be able to
> add new types to the language without having to add new primitive types to
> the system.

When I had this problem, I found no satisfactory type-safe way, and had
resorted to Obj.magic.

Each object type has a common first field which points to a descriptor.
The descriptor contains type id (unique integer) and implementation of
operations common to all objects (in my case it's only application to a
sequence of arguments; for performance there are separate entries for
known small numbers of arguments and one entry taking a list).

Type ids for new types are allocated dynamically, using a global
counter. I ignore the possibility of wraparound, one would have to make
2**(word_size-1) new types (possible, as types can be defined locally
and then forgotten, but improbable).

The OCaml type which can represent an arbitrary object is a record type
consisting of the descriptor only. Other types are longer records, which
are Object.magic'ed to and from the generic type. Having the descriptor
alone you can apply common operations or check the type.

Unfortunately I found no other representation which is as flexible and
as efficient. It must support performing common operations without
knowing the type, taking some type id suitable for indexing a
dictionary. New types must be definable without having to change the
interpreter modules, and they can be defined both in OCaml code and from
my language. Objects should not require much indirection or duplication.

(The interpreter is now dead, I used it to bootstrap a compiler.)

   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners