Behaviour of append (~=)

Daniel Keep daniel.keep.lists at gmail.com
Tue May 30 04:47:28 PDT 2006



Lionello Lunesu wrote:
> Why does the appending to a 'null' array cause the contents to be copied?
> 
> int[] ar1; // some array, not constant
> 
> int[] ar2 = null;
> ar2 ~= ar1;
> // ar2 !is ar1
> 
> But it could be like this:
> if (ar2)
>   ar2 ~= ar1;
> else
>   ar2 = ar1;
> 
> Wouldn't it be a good optimization for ~= to check for null first, to
> prevent the copy?
> 
> L.

I think the problem with this is that it's an edge case.  With your
suggestion, appending to a null array and appending to an *empty* array
would have completely different semantics.  Now, every programmer who
uses arrays has to watch out for this one special case.

Yes, it would be better performance-wise, but it would be hell on
programmers since it's non-obvious behaviour.

	-- Daniel

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/



More information about the Digitalmars-d-learn mailing list