Inheritance and arrays

Arafel er.krali at gmail.com
Mon Jul 3 16:04:54 UTC 2023


On 3/7/23 17:41, Steven Schveighoffer wrote:

> 
> If Java works, it means that Java either handles the conversion by 
> making a copy, or by properly converting on element fetch/store based on 
> type introspection. It also might use a different mechanism to point at 
> interfaces.
> 

As I mentioned in another reply, Java does it by adding a runtime check 
to the type at insertion. This is a performance hit, and I'm glad it's 
not done in D.

Also, I think I read in some other thread here that Java uses a single 
pointer for both objects and interfaces, but I think the biggest issue 
here is the covariance.

>> I find it most unexpected and confusing and, assuming it won't change 
>> anytime soon, I think it should at the very least be **clearly** 
>> documented. And the cast forbidden, since it can't possible work 
>> between classes and interfaces.

I want to answer to myself here: it was my knowledge of type theory that 
was lacking: TIL that even if two types are covariant, arrays of them 
needn't be, and usually aren't. So I was spoiled by Java ;-)

[In case somebody else is 
interested](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Arrays) 
(also just google it, there are tons of links on the subject).

On a final note, thanks to everybody who answered! For all its quirks, D 
is a great language that I enjoy using, and the community here is great, 
even if at times it could be a bit more optimistic :-)



More information about the Digitalmars-d-learn mailing list