Array initialization with common base - Best way?

Bill Baxter wbaxter at gmail.com
Sun Nov 9 15:32:55 PST 2008


On Mon, Nov 10, 2008 at 6:47 AM, Ary Borenszweig <ary at esperanto.org.ar> wrote:
>
> 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)

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