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
Re: [Caml-list] possible to define a type where = is forbidden ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-10-12 (08:02)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] EQ hash tables?
> Actually, this brings me to a question I wanted to ask for a long time:
> while I never used this so far, I just assumed that OCaml does provide
> hash tables where keys are compared w.r.t. "being the same" ('==' , that
> is), rather than only hash tables where keys are compared for "being
> equal" (say, '=').

Easily definable using the functorial interface to hash tables:

module MyEqHashTable =
      type t = my_type_for_keys
      let equal = (==)
      let hash = Hashtbl.hash

> Of course, "EQ hash tables" have to be treated in a slightly special way
> when talking about stop&copy GCing.

No, not "of course".  You're thinking of using the address of a memory
block as its hash value.  This indeed requires GC support.  But you
can get the semantics you want (keys compared by reference) while
still computing the hash code structurally.  This is what the code
snippet above does.

- Xavier Leroy