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
[Caml-list] Hash over ref
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-20 (23:29)
From: Alain Frisch <Alain.Frisch@e...>
Subject: Re: [Caml-list] Hash over ref
On Thu, 20 May 2004, Jon Harrop wrote:

> Is it possible to hash a ref itself, rather than the object it references?

If you really need to use values of type 'a ref (that you create yourself,
but need to store in existing data structures), you can cheat and
implement Damien suggestion like this:

module HashedRef : sig
  val ref: 'a -> 'a ref
  val hash: 'a ref -> int
end = struct
  class c = object end
  type 'a my_ref = { mutable value: 'a; uid : int }

  let ref x = Obj.magic { value = x; uid = (new c) }

  let hash x =
    if Obj.size (Obj.repr x) = 2 then (Obj.magic x).uid
    else failwith "HashedRef.hash: this reference is not hashable"

(Don't do this at home, of course.)

-- Alain

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: