"static" UFCS

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Jun 15 18:18:26 PDT 2012


On 6/14/12, Jacob Carlborg <doob at me.com> wrote:
> Is it possible, somehow, to emulate adding new _static_ methods to a
> class, something like this:

Of course there is, this is D not Java! Enjoy:

import std.stdio;
import std.metastrings;

void fooBar(T : Foo)(int x) { writeln(x); }

struct Foo
{
    static auto opDispatch(string name, Params...)(Params params)
    {
        enum strOf = typeof(this).stringof;
        enum str = Format!("return .%s!%s(params);", name, strOf);
        mixin(str);
    }
}

void main()
{
    Foo.fooBar(4);
}


More information about the Digitalmars-d mailing list