resizeable arrays: T[new]
Sean Kelly
sean at f4.ca
Tue Jun 5 12:31:17 PDT 2007
Sean Kelly wrote:
> Walter Bright wrote:
>> Sean Kelly wrote:
>>>
>>> Well, I think it is very rare for a function to want to resize an
>>> array that isn't passed by reference. My first reaction would be to
>>> disallow resizing of non-ref array parameters entirely. Since
>>> resizing may occur in place, this could be seen as a mutating
>>> operation on the underlying data. If the user wants to resize a
>>> non-ref array parameter for local use he use assign ops and such.
>>
>> That was my first reaction too, but Frits posted a reasonable use case.
>
> Do you mean:
>
> T[] foo(<parameters>, T[] buffer = null) {
> buffer.length = <something>;
> <fill buffer>
> return buffer;
> }
>
> Wouldn't it work if changed to this:
>
> T[] foo(<parameters>, T[] buffer = null) {
> if( buffer.length < newSize )
> buffer = new T[newSize];
> <fill buffer>
> return buffer;
> }
>
> I think it's arguable that the second form is also more meaningful given
> that the array is not passed by reference.
Given the above, am I correct in assuming that it will only be illegal
to resize an array via .length if the underlying data is const? If the
data is not const then it seems completely legitimate to resize the
supplied array, even if doing so means an in-place expansion.
Sean
More information about the Digitalmars-d-announce
mailing list