[mercury-users] Simulating passed data with Mercury/C#

Jonathan Morgan jonmmorgan at gmail.com
Fri Apr 7 14:31:45 AEST 2006


On 4/5/06, Fergus Henderson <fjh-mailbox-58 at galois.com> wrote:
> Yes.  This looks like a good time to use inline C# code
> (e.g. pragma foreign_decl).  The inline C# code would
> declare a class with a field of type Object
>
>         :- pragma foreign_decl("C#", "
>                 class CallBack {
>                         Object my_closure;
>                         public CallBack(Object closure) { my_closure = closure; }
>                         public void callback(...) { call_mercury_closure(my_closure, ...); }
>                 }
>         ").
>
> Then you can use that class in pragma foreign_proc declarations,
> e.g. the one that calls Tcl_CreateCommand() or its equivalent.

Note that if you restrict yourself to C# 2.0 (which we can't) this
could be done simply and elegantly in the foreign_proc with an
anonymous delegate.  It would go something like:

Tk.CreateCommand(..., delegate(...) {
    call_mercury_closure(Closure,...);
}, ...);

This saves the trouble because Closure will be saved until the
delegate itself is garbage collected.  For one callback registration
function there isn't that much difference in complexity between the
two approaches, but any situation with a family of callbacks would
find the anonymous method approach far more straightforward.

Jon

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list