xxxxxxxxxx
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();
}