Possible change to array runtime?

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 13 10:18:05 PDT 2014


On Thu, 13 Mar 2014 12:29:59 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 3/13/14, 8:24 AM, Steven Schveighoffer wrote:
>> Proposal: iff you are setting the length of an array via the length
>> property AND you are setting the length to 0 AND the array starts at the
>> beginning of the block AND the array is mutable (not const or
>> immutable), THEN assumeSafeAppend is called on that array AUTOMATICALLY.
>
> I think this is a sensible proposal, and well argued. However it may  
> break code (even code that arguably wasn't really nicely thought  
> through), and in subtle ways. "We upgraded the compiler, and our 50KLOC  
> app now produces invalid results." Very hard to get working on that, in  
> particular since it's unclear whether this particular matter was the  
> actual culprit.

A good point that the error that would occur, if you depended on setting  
length = 0 to not overwrite your mutable array, would be corruption, and  
that is not a good result. Even if your code was foolish to begin with.

As a mechanism to check this, we could throw an exception when appending  
to an array that has been set to length = 0, but not had assumeSafeAppend  
called on it. This would obviously just be a bug-finding mechanism, and  
not for production.

>
> I think there's a much simpler approach that could help. Define and  
> document a function clearForAppend that resets the length to 0 and puts  
> the array in gear for appending. Maybe an optional parameter indicates a  
> minimum desired capacity.
>
> Then people who want to migrate from D1 to D2 or generally consider  
> using this idiom can grep their source code for "length *= *0" and make  
> the change on their own pace and with appropriate testing.

Arguably this is already possible within a project, such as Sociomantic's.  
They haven't done it yet. I don't know why.

-Steve


More information about the Digitalmars-d mailing list