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 (01:30)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Weak hash table for attaching extra data to an object
On Tue, 2007-08-14 at 21:44 +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 not a precise description. You cannot put arbitrary
meta-data into the Seq nodes with this design, for a given
data structure, you can choose just one type of meta-data: this
is the same as for example a Hashtbl.

If you wish to *dynamically* vary the meta data, which is often
the case when you have a tool like a compiler that performs
phased analysis, a property list based on polymorphic variants
can be useful.

This is one of three ways I can think of to vary the data 
type dynamically across compilation unit boundaries.

A second way is to use mutable field of a class type
and change the object to a different implementation
of the same abstraction.

The third way is quite tricky and equivalent to the first:
use a function unit -> unit as the type, and execute it
to throw an exception. This is basically dynamic typing.

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: