Summary: array of base class cannot be initialized with derived
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Wilfried Kirschenmann <> 
2011-03-09 01:04:46 PST ---

The following example

#!../dmd2/linux/bin/rdmd -unittest

class A{}
class B : A{}
class C : A{}

void main(string[] args){
    B b;
    C c;
    A[] ar = [b, c];

give this error with DMD 2.052 (file is called test.d) :

./test.d(10): Error: cannot implicitly convert expression (b) of type test.A to
./test.d(10): Error: cannot implicitly convert expression ([(__error),c]) of
type C[] to A[]

Indeed, I agree that b cannot be converted to C.
However, I do not understand why the compiler doesn't try to convert c to A.

As a workaround, I found :
A[] ar = [b, cast(A)c];

In fact, all items except the first one have to be casted. 
The other solution is :
A[] ar = [b];

This behavior is inconsistent. I would have expected :
1) all item have to be casted to A
2) no item has to be casted to A
Having just the first element to be implicitly casted is surprising.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to