[Issue 20158] New: Erroneous incompatible types error for classes and interfaces when the class is not directly convertible to the interface but is convertible to a parent of the interface

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 23 21:15:02 UTC 2019


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

          Issue ID: 20158
           Summary: Erroneous incompatible types error for classes and
                    interfaces when the class is not directly convertible
                    to the interface but is convertible to a parent of the
                    interface
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: n8sh.secondary at hotmail.com

Demonstration:

https://run.dlang.io/is/tlyu7Z
---
interface I { void foo(); }
interface J : I { void bar(); }
class C1 : I { override void foo() {} }
class C2 : J { override void foo() {}; override void bar() {}; }

I newI() { return new C1(); }
J newJ() { return new C2(); }
C1 newC1() { return new C1(); }

void main(string[] args)
{   
    const c = args.length > 1;
    I blah;
    // Compiles:
    if (c)
        blah = newJ();
        else
        blah = newC1(); 
    // Fails to compile:
    blah = c ? newJ() : newC1(); // Error: incompatible types for (newJ()) :
(newC1()): J and C1
    // Fails to compile:
    blah = () {
        if (c)
            return newJ();
        else
            return newC1(); // Error: mismatched function return type inference
of C1 and J
    }();
    // P.S.
    // When the class directly implements the interface there is no problem.
    auto _ = c ? newI() : newC1(); 
}
---

--


More information about the Digitalmars-d-bugs mailing list