Function wrapping

Exposing D functions to Python is easy! The heart of Pyd's function wrapping features is the def template function:

void def(alias fn, char[] name = symbolnameof!(fn), fn_t = typeof(&fn), uint MIN_ARGS = minArgs!(fn)) (char[] docstring="");

All calls to def must occur before calling module_init. Any function whose return type and arguments are convertible can be wrapped by def. def can only wrap functions with in arguments (not out or inout or lazy). def also provides support for wrapping overloaded functions as well as functions with default arguments. Here are some examples:

import pyd.pyd;
import std.stdio;

void foo(int i) {
    writefln("You entered ", i);

void bar(int i) {
    writefln("bar: i = ", i);

void bar(char[] s) {
    writefln("bar: s = ", s);

void baz(int i=10, char[] s="moo") {
    writefln("i = %s\ns = %s", i, s);

extern (C) void PydMain() {
    // Plain old function
    // Wraps the lexically first function under the given name
    def!(bar, "bar1");
    // Wraps the function of the specified type
    def!(bar, "bar2", void function(char[]));
    // Wraps the function with default arguments


And when used in Python:

>>> import testmodule
You entered 10
>>> testmodule.bar1(20)
bar: i = 20
>>> testmodule.bar2("monkey")
bar: s = monkey
>>> testmodule.baz()
i = 10
s = moo
>>> testmodule.baz(3)
i = 3
s = moo
>>> testmodule.baz(4, "apple")
i = 4
s = apple