caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Unset or remove an OCaml callback registration
@ 2008-04-02 18:12 Hezekiah M. Carty
  2008-04-03  0:26 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Hezekiah M. Carty @ 2008-04-02 18:12 UTC (permalink / raw)
  To: Caml-list List

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)?

Thanks,
Hez

-- 
Hezekiah M. Carty
Graduate Research Assistant
University of Maryland
Department of Atmospheric and Oceanic Science


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Unset or remove an OCaml callback registration
  2008-04-02 18:12 Unset or remove an OCaml callback registration Hezekiah M. Carty
@ 2008-04-03  0:26 ` Jacques Garrigue
  2008-04-03 14:07   ` Hezekiah M. Carty
  0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2008-04-03  0:26 UTC (permalink / raw)
  To: hcarty; +Cc: caml-list

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
Val_int(0))

Jacques Garrigue


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Unset or remove an OCaml callback registration
  2008-04-03  0:26 ` [Caml-list] " Jacques Garrigue
@ 2008-04-03 14:07   ` Hezekiah M. Carty
  2008-04-03 14:25     ` Gordon Henriksen
  0 siblings, 1 reply; 4+ messages in thread
From: Hezekiah M. Carty @ 2008-04-03 14:07 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Thu, 03 Apr 2008, Jacques Garrigue wrote:

> From: "Hezekiah M. Carty" <hcarty@atmos.umd.edu>
> > 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
> Val_int(0))

If I wrap the C interface in an OCaml module, would it be enough to
include:

let () = Callback.register "foo" 0

in the top level of the module to have this command execute when the
module is loaded?  Or does the module user have to do some explicit
initialization?

Thank you very much for the response,
Hez


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Unset or remove an OCaml callback registration
  2008-04-03 14:07   ` Hezekiah M. Carty
@ 2008-04-03 14:25     ` Gordon Henriksen
  0 siblings, 0 replies; 4+ messages in thread
From: Gordon Henriksen @ 2008-04-03 14:25 UTC (permalink / raw)
  To: caml-list

On Apr 3, 2008, at 10:07, Hezekiah M. Carty wrote:

> On Thu, 03 Apr 2008, Jacques Garrigue wrote:
>
>> From: "Hezekiah M. Carty" <hcarty@atmos.umd.edu>
>>> 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 Val_int(0))
>
> If I wrap the C interface in an OCaml module, would it be enough to  
> include:
>
> let () = Callback.register "foo" 0
>
> in the top level of the module to have this command execute when the  
> module is loaded?  Or does the module user have to do some explicit  
> initialization?


I've used this technique, although I did have initialization ordering  
problems. The outer module is not necessarily initialized before the  
inner one. In my case, the initialization was idempotent, so I simply  
repeated it in each submodule.

— Gordon


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-04-03 14:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-02 18:12 Unset or remove an OCaml callback registration Hezekiah M. Carty
2008-04-03  0:26 ` [Caml-list] " Jacques Garrigue
2008-04-03 14:07   ` Hezekiah M. Carty
2008-04-03 14:25     ` Gordon Henriksen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).