Dynamic Method Dispatch

Derek Ney derek at hipgraphics.com
Sat Jan 20 15:25:39 PST 2007


I am trying to write an interpreter for a dynamic language similar to Ruby using D. I need to dynamically dispatch a method call to an object method. I really just need a method pointer for this. Something like this:

CallMethod(receiver,methodname, methodarg)
{
  methodptr mp = lookup_method(methodname);

  return receiver.mp(methodarg);
}

I just can't see how I can do this with a delegate cause the receiver is dynamic.
I need a table mapping methodname to a method pointer that is not tied
a particular instance. In a sense I could use a delegate if I could do this:

CallMethod(receiver,methodname, methodarg)
{
  delegate dg = lookup_method(methodname);

  dg.ptr = reciever;

  return dg(methodarg);
}

But I do not think you can do that nor is it thread safe nor pretty.
Any ideas of how to do this cleanly? Seems like it should be simple.
If there is a method pointer syntax it would be simple.
-Derek




More information about the Digitalmars-d mailing list