Arrays and inheritance

Monkey Forums/Monkey Bug Reports/Arrays and inheritance

muddy_shoes(Posted 2012) [#1]
Can something be done about making array typing comprehend the class hierarchy? Right now the rules for what works and what doesn't are pretty limiting and quite hard to understand. See example below.




marksibly(Posted 2012) [#2]
Hi,

The main problem here is that in Monkey, arrays can't be 'upcast' (or downcast) the way they can in C#/Java, eg: this is illegal:

Local tmp:Base[]=New Derived[size]

For this reason, I'm reluctant to make auto arrays deduce their type, eg:

[New Base,New Derived1,New Derived2] 'array type is Base[]?

...as it sort of implies this should work...

Local tmp:Base[]=[New Derived1,New Derived2] 'Error! Array type is Derived1[]

But it wont. Also, if none of the elements match the element type of the array, you're hosed anyway. Or you have to cast one of them.

But perhaps that's OK? It shouldn't be too hard to add array type deduction anyway.


muddy_shoes(Posted 2012) [#3]
I can live without auto-deduction of undeclared arrays. I'm okay if you just throw an error "Can't deduce type of mixed array" if it's a problem. However, we need some syntax for saying "here's an array literal and I intend the elements to be of this type" if that's the case.

If I declare an array of elements of type "Base" it would be nice to be able to populate a literal declaration with derived type instances without having to cast them to the base type. I'm explicitly telling Monkey what the array is and checking that the elements are of that type or derivations shouldn't be impossible. The same goes for instances where an array literal is being defined as a parameter to a method/function. The method/function signature explicitly states what the array is meant to be, after all.