On 03.03.2010 5:51, dsimcha wrote:
I consider the function-delegate thing a reasonably solved problem, starting in
the next release of Phobos.  See:

http://dsource.org/projects/phobos/changeset/1428

Your code works only if return/parameter types are accessible from std.functional. An implementation that handles any types is way more complex.

You'll have to extract storage classes from the function pointer type and then reconstruct the delegate type using those extracted storage classes combined with the parameter types as elements of the parameter type tuple. An example demonstrating what is meant:

auto toDelegate(F)(F fp) {
    ...
    alias ParameterTypeTuple!(F) Params;

    // CTFE generating the delegate type string
    static string genDgStr() {
        auto paramSCs = extractParamSCs!(F);
        auto dgStr = extractReturnSCs!(F) ~ " ReturnType!(F) delegate(";
        foreach (i, paramSC; paramSCs) {
            if (i) dgStr ~= ", ";
            dgStr ~= paramSCs[i] ~ " Params[" ~ to!string(i) ~ "]";
        }
        return dgStr ~ ")";
    }

    mixin ("alias " ~ genDgStr() ~ " Dg;");
    ...
}



So, for the function type 'ref SomeAlienStruct function(ref SomeAlienStruct s)', the mixed-in delegate type alias will be:

alias ref ReturnType!(F) delegate(ref Params[0]) Dg;

Note that there are no stringof's involved in the alias declaration.

Terrible.





Reply via email to