[Issue 4989] New: opDispatch not used when alias this is present

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 4 10:38:49 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4989

           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
        AssignedTo: nobody at puremagic.com
        ReportedBy: schveiguy at yahoo.com


--- Comment #0 from Steven Schveighoffer <schveiguy at yahoo.com> 2010-10-04 10:38:27 PDT ---
Example:

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.foo();
  s.foo2();
  // 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
this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list