Pyd: Using the new mixins

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Feb 17 00:14:35 PST 2007


Revision 100 of Pyd uses the new mixins to automatically generate "shim" 
classes, one of which must be made for every class which is exposed to 
Python. (Revision 100 should be considered semi-broken, since I got all 
the big stuff done, but a few details need to be gone over. Also, it 
requires DMD 1.005, which GDC is not yet up to par with, so Linux 
support is currently broken.)

There's little to show for this. The major effect of revision 100 is a 
vast /reduction/ in code. Wrapping a class is now as simple as:

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

extern(C) void PydMain() {
     module_init();
     wrap_class!(
         Foo,
         Def!(Foo.bar)
     );
}

This now properly handles all polymorphic behavior between D and Python:

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

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

 >>> # And in Python's interactive mode
 >>> p = PyFoo()
 >>> polymorphic_call(p)
PyFoo.bar

And /that/ is quite something.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list