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
Unset or remove an OCaml callback registration
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-04-03 (00:26)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Unset or remove an OCaml callback registration
From: "Hezekiah M. Carty" <hcarty@atmos.umd.edu>

> Is it possible to unregister a function or other value registered with
> Callback.register?  I use (Callback.register "foo" some_func) to
> register coordinate transforms for a C library.  If no callback is
> registered for "foo" then a default transform is used by the C
> library.  I would like to be able to set and then remove the
> association of some_func to the name "foo" at various points in my
> program so that the C code will fall back on the default transform
> (which does not require a callback and is much faster) when the named
> callback "foo" is undefined or has been unregistered.
> Is this possible, either from the C or OCaml side without making the
> callback associate with "foo" an option type (use (Callback.register
> "foo" (Some some_func)) to set a callback and (Callback.register "foo"
> None) to clear it)?

Since Callback.register has type: string -> 'a -> unit
you are not limited by the ocaml type system.
So you can reset your value with
  Callback.register "foo" 0
and check for equality with Val_int(0) on the C side.
(Note that you must initialize the value to 0 at program startup,
because the default for an unitialized value is 0 which is not

Jacques Garrigue