Inheritance and arrays

Arafel er.krali at gmail.com
Mon Jul 3 09:50:20 UTC 2023


Hi!

I am a D user coming from java, rather than from C/C++ (although 
obviously also have some exposure to them), and thus apparently one of 
the few people here who likes OO (within reason, of course).

So while I appreciate the fact that D closely follows java's design, I 
wonder why there is no implicit inheritance for arrays (also the same 
applies to AAs):

```d
interface I {}
class C : I {}

void main() {
     I i;
     C c = null;
     i = c; // Works

     I[] ii;
     C[] cc = null;
     // ii = cc; // Doesn't work: Error: cannot implicitly convert 
expression `cc` of type `C[]` to `I[]`
     ii = cast (I[]) cc; // Works, but why do I need to cast?
}
```

The equivalent java code compiles without issue:

```java
interface I {}
class C implements I {}

public class MyClass {
     public static void main(String args[]) {
         I i;
         C c = null;
         i = c; // Works

         I[] ii;
         C[] cc = null;
         ii = cc; // Also works
     }
}
```

Is this a conscious design decision (if so, why?), or just a leak of 
some implementation detail, but that could eventually be made to work?


More information about the Digitalmars-d-learn mailing list