[Issue 3543] [tdpl] ternary operator can't find common type for classes/interfaces

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Apr 22 10:32:36 PDT 2014


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

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #14 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Andrei Alexandrescu from comment #11)

> A rule that would avoid that issue would be to count all common ancestors
> and find the one that is closest to both, i.e. shortest sum of steps from
> both interfaces. I thought of it but eliminated it because it can create
> rather subtle distinctions.
> 
> I agree the problem is not as clear cut as we'd like.

It's pretty clear cut to me:

1. If you have a single common root, that is it. Any roots that are
possibilities only because they are ancestors of another root do not count.
2. If you have multiple common roots, it's ambiguous, a cast is required.

Note, this has to be considered: Currently in D, although it is actually
impossible to have a non-D object implement a D interface, interfaces are not
derived from Object. So technically, Object would always be a root separate
from any interface, which would make virtually any expression that involves
interfaces ambiguous. I think in at LEAST this context (though I would argue
for complete acceptance of interfaces always deriving from Object), Object
should be considered a root of all interfaces, thereby allowing interfaces to
validly be the result.

Note, I would make also an exception for literal array expressions to take the
usage into consideration. Currently, this does not work, but I think it should:

interface A {}
class B : A {}
class C : B {}
class D : B {}

void main()
{
   wstring ws = "hello"; // ok, compiler knows the literal should be typed as
                         // wstring.
   A[] a = [new C, new D]; // error, [new C, new D] is typed as B[]
}

--


More information about the Digitalmars-d-bugs mailing list