Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Weak hash table for attaching extra data to an object
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-15 (19:04)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Weak hash table for attaching extra data to an object
On Tue, Aug 14, 2007 at 09:44:15PM +0100, Jon Harrop wrote:
> You can also parameterize the type over metadata:
>   type 'a expr =
>     | Int of int
>     | Var of string
>     | Seq of 'a seq
>   and 'a seq = {h: 'a expr; t: 'a expr array; meta: 'a};;
> Now you can insert arbitrary metadata into Seq nodes.

This is basically what PXP does, and what my own PG'OCaml does.
Unfortunately it's not very usable for a few practical reasons, as I
shall describe.

Firstly it only allows a single annotation.  You can use a tuple to
get around that, but that requires the annotators to cooperate
somehow, which is not much good if you're combining code from several

Secondly if you don't want to annotate, you need to manually cast the
type to 'unit t' somewhere in your main program (and that will fail if
one of the libraries you're using wants to annotate the type).

Also, can you safely annotate the nodes one way in one part of your
program, and other way in another part (eg. if you're transforming a
tree with annotations, in stages)?  Using variants isn't completely
safe (but then neither are WHTs in this instance because you can not
annotate a node and WHTs don't catch this statically).

> If your metadata are sporadically used or, in particular, only sparsely 
> present or you do not have access to the library then the weak hash table 
> approach is probably the way to go.

I should have said :-) I'm trying to annotate the <img> & <applet>
nodes in a typical HTML document, so they are both sparse (compared to
all nodes), but there are many of them.


Richard Jones
Red Hat