[Issue 12283] Array literals of interface implementations do not resolve to interface

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jul 13 05:15:57 PDT 2017


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

Steven Schveighoffer <schveiguy at yahoo.com> changed:

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

--- Comment #3 from Steven Schveighoffer <schveiguy at 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.

--


More information about the Digitalmars-d-bugs mailing list