Thread overview
[Issue 3543] [tdpl] ternary operator can't find common type for classes/interfaces
Dec 17, 2022
Iain Buclaw
Oct 07, 2023
Paul Backus
Jan 17, 2024
Dlang Bot
Feb 07
Dlang Bot
April 22, 2014
https://issues.dlang.org/show_bug.cgi?id=3543

Steven Schveighoffer <schveiguy@yahoo.com> changed:

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

--- Comment #14 from Steven Schveighoffer <schveiguy@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[]
}

--
July 17, 2014
https://issues.dlang.org/show_bug.cgi?id=3543

--- Comment #15 from bearophile_hugs@eml.cc ---
The same problem found by "Uranuz": http://forum.dlang.org/thread/xgxgfwltboextlmfjhgj@forum.dlang.org


interface IBase {}
class Impl(T): IBase {
    T value;
}
void main() {
    IBase a = true ? (new Impl!uint) : (new Impl!string);
}



test.d(6,23): Error: cannot implicitly convert expression (new Impl!uint) of type object.Object to test.IBase

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=3543

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4

--
October 07, 2023
https://issues.dlang.org/show_bug.cgi?id=3543

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=24178

--
January 17, 2024
https://issues.dlang.org/show_bug.cgi?id=3543

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #16 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright updated dlang/dmd pull request #16051 "fix Issue 3543 - [tdpl] ternary operator can't find common type for c…" fixing this issue:

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

https://github.com/dlang/dmd/pull/16051

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

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #17 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #16051 "fix Bugzilla Issue 3543 - [tdpl] ternary operator can't find common type for c…" was merged into master:

- 7f613ea4ca4cfa7d57848ab7c26d357a9d71a93a by Walter Bright:
  fix Bugzilla Issue 3543 - [tdpl] ternary operator can't find common type for
classes/interfaces

https://github.com/dlang/dmd/pull/16051

--