Array type conversion

James Dennett jdennett at acm.org
Sat Apr 28 23:42:22 PDT 2007


Walter Bright wrote:
> James Dennett wrote:
>> An array of (pointers/references to) derived is usable
>> as an *immutable* array of base (for suitable English
>> meaning of immutable, matching C++'s notion of the
>> array (equivalently, the pointers it contains) being
>> const.
>>
>> Java has runtime checks required because it allows
>> conversion from array of Derived to array of Base,
>> and that (as you know) also uses reference semantics.
>> The conversion is widely viewed as a mistake in Java;
>> if I pass a Derived[] around, the language should
>> not silently allow one of its elements to refer to
>> a Base object.
> 
> But a derived reference can always be implicitly converted to a base
> reference anyway. That's the point of polymorphism.

That's not the issue here either.  One more level of
indirection is present when dealing with arrays of
references or references to references.

The point is that a reference to a derived reference
must *not* be converted to a reference to a base
reference, just as an array of derived references
must not be converted to an array of base references
in case any is changed to a reference to an object
that is not a derived.

-- James



More information about the Digitalmars-d mailing list