Thread overview
[Issue 12283] Array literals of interface implementations do not resolve to interface
Dec 17, 2022
Iain Buclaw
Oct 07, 2023
Paul Backus
July 13, 2017
https://issues.dlang.org/show_bug.cgi?id=12283

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |schveiguy@yahoo.com
         Resolution|DUPLICATE                   |---

--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> ---
Turns out it's not the same.

In the latest DMD (2.074.1):

version(bad) interface A {}
else class A {}
class B : A {}
class C : A {}

void main()
{
   A[] arr = [new B, new C];
}

when compiled with version=bad:
test12283.d(9): Error: cannot implicitly convert expression ([new B, new C]) of
type Object[] to A[]

Without the version, it works.

My guess is the inference of the type only takes into account the expression, not the fact that it's an initializer. However this is surprising behavior, because in other cases, an initializer does use the type of the variable being initialized to work:

auto x = [1, 2]; // int[]
short[] y = x; // Error
short[] z = [1, 2]; // But this is OK

The only way to get it to work is to cast one element to an A. An ugly error-prone mechanism (which also happens to do a dynamic cast in some cases).

I think this should work if the variable being declared has a type and isn't auto. If it's auto, then the behavior of making an Object[] is a valid answer.

--
July 13, 2017
https://issues.dlang.org/show_bug.cgi?id=12283

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P3

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

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

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

--