deadimp escribió:
That's the problem I had mentioned, though - the type of the entire array being
inferred from the first element rather than the context (i.e. what it's
initializing if not declared as 'auto') or subsequent elements (which would be
a bit difficult...).
When I try that, the compiler prints this error message: (formatted for this
context)
"Error: cannot implicitly convert expression (new BadChild) of type BadChild to
GoodChild"
I'm not sure what a good syntax replacement would be. C# / Java style new array
instantiations wouldn't really fit in syntactically with the already
established style of arrays. But maybe there's a way the type of an array coud
be explicitly stated without having to cast to make the type inference correct.
Jérôme M. Berger Wrote:
I would replace the last line with:
Base[] list = [ new GoodChild, new BadChild, ...];
It's clearer, there's less typing and it won't suddenly fail if you
remove the first element and accidentally remove the cast at the
same time or if you add a new first element and forget to add the
cast...
Jerome
Can't the compiler try to set the array's component type to the most
general type of it's elements, by combining the most general types by pairs?
Hmmm... now that I think it, it's not that easy because of interfaces.
Did this subject was ever discussed? If not, I'll try to think of an
algorithm for this. (because if it was discussed, there must be a good
reason for not doing this)