Partial classes & forward declarations

Martin Nowak dawg at dawgfoto.de
Sat Apr 21 09:21:55 PDT 2012


> I have a class, and it contains various method declarations. These  
> methods
> are to be implemented via mixins which generate stubs binding it to some
> complex C++ call-through mechanism.
> My approach was to allow the user to forward declare the methods, which
> will tell the compiler all the information about the function call, and
> then later on, a mixin would scan the class for such methods, and produce
> the actual function stubs accordingly.

Not sure if I understood you problem correctly,
but these are the two idioms I use regularly.

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
module lib;

mixin template genFuncAlias(string fname, RT, Args...)
{
     RT __genFunc(Args args)
     {
         //...
         return RT.init;
     }

     mixin("alias __genFunc "~fname~";");
}

mixin template genFuncName(string fname, RT, Args...)
{
     enum __s = codegen(fname);
     mixin(__s);
}

string codegen(string fname)
{
     import std.array, std.format;
     auto app = appender!string();
     formattedWrite(app,
q{
     RT %1$s(Args args)
     {
         //...
         return RT.init;
     }
}, fname);
     return app.data;
}

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
module client;
import lib;

class Blah
{
     // not overloadable ??
     mixin genFuncAlias!("bar", int, string, double);
     // mixin genFuncAlias!("bar", int, double, string);

     mixin genFuncName!("foo", int, string, double);
     mixin genFuncName!("foo", int, double, string);
}

void main()
{
     auto b = new Blah();
     b.bar("1.0", 1.0) && assert(0);
     // not overloadable ??
     // b.bar(2.0, "2.0") && assert(0);
     b.foo("1.0", 1.0) && assert(0);
     b.foo(2.0, "2.0") && assert(0);
}

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dmd -run client lib


More information about the Digitalmars-d mailing list