DIP 1025--Dynamic Arrays Only Shrink, Never Grow--Community Review Round 1

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 11 17:38:53 UTC 2019


On 11/11/19 12:28 PM, Jonathan M Davis wrote:
> On Monday, November 11, 2019 10:12:13 AM MST Ola Fosheim Grøstad via
> Digitalmars-d wrote:
>> Anyway, it also isn't true that Dynamic Arrays don't have a type.
>> It isn't untyped. But the typesystem it uses is completely
>> different from the rest of the language.
>>
>> It appears to be quasi-dynamic effects based, whereas the rest of
>> the language is mostly static strongly typed. Meaning, the type
>> changes as you use it. Does any other object in D do that?
> 
> How on earth do D dynamic arrays change their type? int[] is always int[]. I
> believe that the bit that Steven is talking about with regards to them being
> untyped is that inside druntime, it uses void*, because the code isn't
> templated - which arguably is something that should be fixed. Outside of
> druntime's innards though, a dynamic array always has the same type. All it
> really is is a struct containing a pointer and a size_t for length.

auto arr = "hello".dup;

ubyte[] arr2 = cast(ubyte[]) arr;

arr2 ~= 1; // fine appends in-place (possibly)
arr ~= 'a'; // fine, makes a copy

The type doesn't matter to druntime, only the length of data. That is 
the point.

Of course, this all breaks down if the type has a postblit/destructor, 
which IS stored by druntime. But I'm not sure if there are any runtime 
mechanisms to prevent it (I haven't touched that code much since the 
posblit/dtor code was added).

-Steve


More information about the Digitalmars-d mailing list