English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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 = Oo.id (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"
end

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

-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners