When is a dynamic array really a static array?

Eugene Wissner belka at caraus.de
Tue Dec 31 11:51:26 UTC 2019


On Tuesday, 31 December 2019 at 11:20:31 UTC, Patrick Schluter 
wrote:
> 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.

It is inconsistent. The compiler can't just do whatever it wants. 
If typeof(x[]) says that the type is char[], then the type should 
be char[]. How am I supposed to reason about the code if I don't 
even know the types of my variables, and what overloads are 
called? There should be strict coercion rules and there is no 
such rule for converting a dynamic array to a static one just 
because the compiler knows. And it changes the behaviour if the 
variable is used in one of the templates.


More information about the Digitalmars-d mailing list