I know in Java arrays are covariant. So for example:
Assume Dog is a subclass of Animal
In java the arrays are covariant making: Animal[] a supertype of Dog[]
But in java generic collections are not covariant such as:
ArrayList<Animal> is not a supertype of ArrayList<Dog>
My question is are arrays in Ada Covariant?
I think that by "Animal[] [is] a supertype of Dog[]" you mean that
Animal[42]
might in fact be aDog
? If so, then the answer is No.In Java, variables (including array elements) are in fact references (think pointers).
Given
you can of course say
but then all the elements have to be
Animals
.To get dispatching in Ada, you have to have a class-wide value to dispatch on, so you could try
but then the compiler would tell you
(
Animal
andDog
objects aren't the same size). You could trywhich allows you to say
but then you're left with memory management problems, because Ada doesn't do garbage collection (at least, with any of the native code compilers I'm aware of). You could save yourself a lot of grief by using
Ada.Containers.Indefinite_Vectors
.