opDispatch() is pretty damn great
Brad Anderson
eco at gnuk.net
Wed Jan 8 15:27:36 PST 2014
On Wednesday, 8 January 2014 at 23:20:06 UTC, Szymon Gatner wrote:
> Still exploring what D has to offer but this blew my mind:
>
> <snip>
>
> I don't quite understand why "enum name" part is necessary (as
> my understanding is that "op" is compile-time constant anyway)
> but since I am crap at D that is what worked for me. I was
> thinking for log time how great something like that would be in
> C++ and I just tried this id D... Mind blown...
> std::reference_wrapper<> would be a zillion times more usable
> with equivalent of opDispatch() this powerful.
There is also `alias this` for subtyping.
import std.stdio;
struct Base
{
void print(string text)
{
writeln("Base : " ~ text);
}
int add(int a, int b)
{
return a + b;
}
}
struct Wrap
{
Base base;
alias base this;
}
void baseOnly(Base b)
{
b.print("passed in a Wrap!");
}
int main(string[] argv)
{
Wrap wrap;
wrap.print("wrapped call, magic!");
baseOnly(wrap);
auto res = wrap.add(1, 5);
return 0;
}
More information about the Digitalmars-d-learn
mailing list