Simon Peyton-Jones wrote:
> [...]
> foreign export dynamic
> mkFooCB :: (Int -> Int -> IO ()) -> IO Addr
>
> freeHaskellFunctionPtr :: Addr -> IO ()
>
> Now if you want the callback itself to free itself, you say
>
> do {
> a <- fixIO (\a -> mkFooCB (callBack a));
Hmmm, is this different from my "let addr = mkFooCB (myCallback addr)"?
If it is not, your example is exactly the second possibility in my
previous mail.
> register a with the window system
> }
>
> callBack :: Addr -> Int -> Int -> IO ()
> callBack a x y = do { unregister a with window system
... and do a "freeHaskellFunctionPtr a"
> ... whatever .. }
>
> Isn't that enough?
That's even too much... :-) The Addr stuff and freeHaskellFunctionPtr
are artefacts of the Haskell binding and should not be exposed to the
API's user. Consequently the callback's type should simply be
type FooAction = Int -> Int -> IO ()
callBack :: FooAction
and the registration function's type should be
registerFoo :: Maybe FooAction -> IO ()
where Nothing means "unregister from action foo". No Addrs here, and
the user does not have to know about freeHaskellFunctionPtr at all.
The need for some kind of global tables arises from the fact that the
binding itself must remember the last registered callback and
unregister/free it before the new one is registered.
Cheers,
Sven
--
Sven Panne Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen Oettingenstr. 67
mailto:[EMAIL PROTECTED] D-80538 Muenchen
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne