Static associative array expressions

Stefan Koch uplink.coder at googlemail.com
Mon Sep 20 13:32:50 UTC 2021


On Monday, 20 September 2021 at 13:08:53 UTC, Steven 
Schveighoffer wrote:
> On 9/19/21 10:10 AM, Stefan Koch wrote:
>> On Sunday, 19 September 2021 at 11:39:15 UTC, pineapple wrote:
>>> This could be nice. It's always been kind of annoying having 
>>> to put AAs in static initializers and not being able to 
>>> inspect them at compile time.
>> 
>> Be careful though.
>> The runtime does not know if an array is in static memory or 
>> not.
>> (Though it would be trivial to add a flag so it knows.)
>> And Therefore when you do this:
>> ```d
>> static mutable_ct_createdAA = mixin(["a","b"]);
>> ```
>> and you mutate that array in any way.
>> The runtime will crash.
>> As it tries write into memory it is not supposed to write in.
>
> How do statically initialized mutable dynamic arrays work? 
> Should be the same thing.
>
> -Steve

I just had a look to what druntime does.
writing into statically allocated memory is fine as long as it's 
allocated writable.
Which means assigning to a mutable array is fine.
And the only time when an array has to reallocate is on appending 
in shrinking.
At which point it is copied. Which is the same for statically 
allocated and dynamically allocated arrays.

However for an AA assigning to an index. may pr may not 
reallocate them.
which is why we would have to put a guard into the assignment 
code as well.


More information about the Digitalmars-d mailing list