When is a dynamic array really a static array?

Patrick Schluter Patrick.Schluter at bbox.fr
Tue Dec 31 11:20:31 UTC 2019


On Monday, 30 December 2019 at 18:17:31 UTC, Steven Schveighoffer 
wrote:
> On 12/30/19 11:45 AM, MoonlightSentinel wrote:
>
>> Digger blames https://github.com/dlang/dmd/pull/4779
>> 
>> This is probably a bug but DMD sometimes detects when the 
>> sliced array is static and uses this information to enable 
>> certain rewrites IIRC, e.g. when initializing another static 
>> array
>
> Thanks for everyone's replies:
>
> https://issues.dlang.org/show_bug.cgi?id=20472
>

Is it really a bug?

A static array is an array where the compiler handles the 
pointer/length structure (i.e. they are known at CT) vs a dynamic 
array, where it is a runtime variable. In contexts where the 
compiler can deduce completely the type (lifetime of the values) 
it can be justified to make CT values out of them.
While I understand that it can be surprizing that the passed 
dynamic array becomes a static array again, it is imho only a 
sign that the compiler was able to deduce completely the lifetime 
of the passed object.

I'm sure it is a good thing even if it might be surprizing in 
some contexts, but it is in the vein of the basic idea behind the 
D language to try to resolve things at CT when possible.

Just my 2 cents.




More information about the Digitalmars-d mailing list