Summary: opDispatch not used when alias this is present
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Steven Schveighoffer <> 2010-10-04 
10:38:27 PDT ---

import std.stdio;

struct X
    int foo() { return 1; }
    bool foo2() {return true;}

struct S

    X x;

    // override only int functions
    auto opDispatch(string fn, Args...)(Args args) if (is(typeof(mixin("x." ~
fn ~ "(args)")) == int))
        writeln("calling " ~ fn);
        mixin("return x." ~ fn ~ "(args);");

    // override functions that aren't supported by X
    void opDispatch(string fn, Args...)(Args args) if (!is(typeof(mixin("x." ~
fn ~ "(args)"))))
        writeln("invalid function " ~ fn);

    // let all others pass through (i.e. foo2)
    alias x this;

void main()
  S s;;
  // s.baz(); // compiler error "Error: no property 'baz' for type 'X'"

when compiled, this outputs nothing.  I'd expect to see (if last line of main
is uncommented):

calling foo
invalid function baz

Note the glaring use case here is being able to have an implicit cast, yet
override the behavior of the implicitly casted member.

At the very least, opDispatch should be used when the alias this'd value does
not support the function.

IMO, opDispatch should be preferred over the alias this'd member.  Simply
because it's possible (though ugly) to select which opDispatch functions
compile, but it's impossible to selectively pick members to compile via alias

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to