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

Jonathan Morgan jonmmorgan at gmail.com
Thu Apr 6 15:39:34 AEST 2006


On 4/5/06, Fergus Henderson <fjh-mailbox-58 at galois.com> wrote:
> On 18-Mar-2006, Jonathan Morgan <jonmmorgan at gmail.com> wrote:
> > I have been looking at graphical bindings for Mercury/IL, and the
> > bindings from C to C# do not appear to support passing data to
> > callbacks very well.
> >
> > The first binding I looked at was Ticklesharp, as it would seem fairly
> > simple to add IL support to mtcltk.  However, this relies on passing
> > the Mercury closure to call as data to Tcl_CreateCommand.  There is no
> > data passed by TickleSharp, so my "generic" handler  would need to
> > know both the name of the command that had been invoked and the
> > Mercury closure to call.
> >
> > Next I looked at Gtk#.  It too does not take user data that is then
> > passed to the callback that has been registered.  The response I got
> > was that "The Gtk# paradigm for the user_data parameter is to create
> > your delegate from an instance method on a class and hold the data
> > member in an instance field."  I don't believe that Mercury can do
> > that, as Mercury foreign procedures in C# are all in static functions,
> > that cannot access instance variables.
> >
> > So the question is, is there any way that we can simulate this user
> > data so that we can register the same callback with different pieces
> > of data associated, and then use that data?  Is it possible to create
> > our own class with the call-back method and instance variables, and
> > then register the callback from that class, but still call the static
> > Mercury-exported functions?
>
> 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.

Thanks.  I wasn't entirely sure of the elegance of such a solution,
but it seemed to work.  You can see the result at
http://www.cs.mu.oz.au/research/mercury/mailing-lists/mercury-reviews/mercury-reviews.200603/0128.html.

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