www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17710] New: Undefined behaviour and non-working casting of

https://issues.dlang.org/show_bug.cgi?id=17710

          Issue ID: 17710
           Summary: Undefined behaviour and non-working casting of
                    overloaded methods invoking overloaded delegates
           Product: D
           Version: D2
          Hardware: x86
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kamil.subzda nexwell.eu

Created attachment 1652
  --> https://issues.dlang.org/attachment.cgi?id=1652&action=edit
Sample code presenting the bug.

Sample code to present the case (also in the attachment):

//

alias BoolFirst = void delegate(bool b, string s);
alias StringFirst = void delegate(string s, bool b);

class Caller {
    void call(BoolFirst bs) { bs(true, "text"); }
    void call(StringFirst sb) { sb("text", true); }
}

class Writer {
    import std.stdio;
    void write(bool b, string s) { writeln("bool+string:", b, "/", s); }
    void write(string s, bool b) { writeln("string+bool:", s, "/", b); }
}

void main() {
    new Caller().call(&new Writer().write);
}

//

The call in main() is ambiguous, but nevertheless the code compiles and the
program runs the "string+bool" variant. Swapping write() methods in the source
file causes calling "bool+string" variant. Casting call(cast(StringFirst)
&w.write) compiles, although is not necessary, because not casting works the
same way, but casting call(cast(BoolFirst) &w.write) - which should help in
calling bool+string variant - does not compile, and the compiler says that
"Caller.call called with argument types (void delegate(bool b, string s))
matches both (...)", which is untrue. Moreover, swapping write() methods again
causes the exact opposite behaviour: cast(BoolFirst) compiles, but is useless,
and cast(StringFirst) does not compile at all.

--
Aug 01 2017