Dear All,
This is the first time i see this "Magic" code... I could understand how it works, but
I've never seen this kind of logic before...is it common?
The magic i'm referring to, is that when you call "
The compiler takes the array of length Zero, get rid of it, and builds a new one of the correct size
and assigns it to 's'.
Why this? why not simply have a call to a.toArray() and have it done atomatically?
I've would expect an Error thrown at run time saying that the array is not big enough...but
not just a silent replacement...
The API documentation says:
the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
In addition to the above, I feel this could be quite dangerous, as shown in the following example:
The code below, creates an Array of size '5'. Then two values are added, one at position [0] and
one at position [4]. When we run the 'a.toArray(s)' method, fields are overwritten and not added...
I wonder why there is a mix. I would have expected Either "Completely Replaced" or Either "Added",
but not a mix of the two. This leaves the array in an inconsistent state...
Thank you for any clarification on this!
Dave