Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Looking up exceptions in a dictionary
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Looking up exceptions in a dictionary
> I haven't found the exception representation described. Is the
> following description accurate?
> 
> An exception is a block whose first field is a pointer used to
> distinguish among exception constructors, and the rest are arguments.
> The constructor pointer points to a one-field block which contains
> a string pointer containing the constructor name.

That's 100% accurate.

> I think the dictionary can be constructed as I say below. Can it be
> done in a better way?
> 
> Since the real thing to be looked up is a pointer, and pointers don't
> have a stable hash, the first step is a Hashtbl.t indexed by string
> values - names of constructors, to quickly narrow the search. Names
> are insufficient to reliably distinguish exceptions, so under each
> name there is an association list holding actual constructor pointers
> for all exceptions having a given name, looked up using List.assq.

You can use the "functorial" interface to Hashtbl to indicate that you
want keys to be compared by address and not by structure:

module ExnHashtbl = 
  Hashtbl.Make(struct type t = string
                      let equal = (==)
                      let hash = Hashtbl.hash
               end)

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr