DeRailed DSL (was Re: compile-time regex redux)

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Feb 10 17:14:01 PST 2007


Bill Baxter wrote:
> Speaking of which I'm surprised Kirk hasn't piped in here more about how 
> this could make life easier for PyD (or not if that's the case).  Any 
> thoughts, Kirk?  You're in one of the best positions to say what's a 
> bottleneck with the current state of compile-time reflection.
> 
> --bb

One area of Pyd which I am unhappy with is its support for inheritance 
and polymorphic behavior.

http://pyd.dsource.org/inherit.html

Getting the most proper behavior requires a bit of a workaround. For 
every class that a user wishes to expose to Python, they must write a 
"wrapper" class, and then expose both the wrapper and the original class 
to Python. The basic idea is so that you can subclass D classes with 
Python classes and then get D code to polymorphically call the methods 
of the Python class:

// D class
class Foo {
     void bar() { writefln("Foo.bar"); }
}

// D function calling method
void polymorphic_call(Foo f) {
     f.bar();
}

# Python subclass
class PyFoo(Foo):
     def bar(self):
         print "PyFoo.bar"

# Calling D function with instance of Python class
 >>> o = PyFoo()
 >>> polymorphic_call(o)
PyFoo.bar

Read that a few times until you get it. To see how Pyd handles this, 
read the above link. It's quite ugly.

The D wrapper class for Foo would look something like this:

class FooWrapper : Foo {
     mixin OverloadShim;
     void bar() {
         get_overload(&super.bar, "bar");
     }
}

Never mind what this actually does. The problem at hand is somehow 
generating a class like this at compile-time, possibly given only the 
class Foo. While these new mixins now give me a mechanism for generating 
this class, I don't believe I can get all of the information about the 
class that I need at compile-time, at least not automatically. I might 
be able to rig something creative up with tuples, now that I think about 
it...

However, I have some more pressing issues with Pyd at the moment 
(strings, embedding, and building, for three examples), which have 
nothing to do with these new features.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list