Array initialization with common base - Best way?

deadimp deadimp at gmail.com
Sun Nov 9 19:50:10 PST 2008


Mebbe the naiive expectations arise because most compilers (those for D, C++, C#, C, Java) take the expression `15 + 0.6` and returns a double - the highest general 'blanketing' type - even though the first operand is an int. I forgot what this concept was called, though... 

I would at least expect it when the variable has the type explicitly declared (`Base[] list`), since the following list would be seen as an initialization... Or is there another way that you initialize arrays, be they static or dynamic?

Bill Baxter Wrote:

> I'm not sure what the problem is.  I think Walter likes the current
> scheme because it's simple for the compiler and simple to explain.
> Unfortunately I think it is counter to naive expectations, which means
> that even though it's simple to explain, it *has* to be explained to
> the uninitiated.
> NumPy works the way you suggest.  It picks the most generic type that
> can hold the answer.  I'm not sure what the algorithm is but it seems
> to give adhere much better to the principle of least surprise.   But
> maybe the fact that NumPy only cares about numeric types makes the
> problem easier.  But I should would like it if this worked
>      auto foo = [1,1,1,0.5];
> and gave me a double[].
> Anyway, that behavior seems to do what most people expeect.  On the
> other hand I've seen this topic of picking the first element's type
> come up several times here, generally because people didn't expect
> that behavior..
> 
> --bb



More information about the Digitalmars-d-learn mailing list