Thread overview
[Issue 4989] New: opDispatch not used when alias this is present
Nov 30, 2010
Simen Kjaeraas
October 04, 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@puremagic.com
        ReportedBy: schveiguy@yahoo.com


--- Comment #0 from Steven Schveighoffer <schveiguy@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: -------
November 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4989


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pelle.mansson@gmail.com


--- Comment #1 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-11-30 03:19:43 PST ---
*** Issue 4224 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4989


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #2 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-11-30 05:25:15 PST ---
*** This issue has been marked as a duplicate of issue 4224 ***

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