opDispatch() is pretty damn great
Szymon Gatner
noemail at gmail.com
Wed Jan 8 16:00:19 PST 2014
On Wednesday, 8 January 2014 at 23:27:37 UTC, Brad Anderson wrote:
> 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;
> }
I was actually thinking about subtyping too but I don't know how
to do alias this with opDispatch() call forwarding to Base from
Wrap
More information about the Digitalmars-d-learn
mailing list