When is a dynamic array really a static array?

MoonlightSentinel moonlightsentinel at disroot.org
Mon Dec 30 16:45:16 UTC 2019


On Monday, 30 December 2019 at 15:39:00 UTC, Steven Schveighoffer 
wrote:
> What do you think this should print?
>
> import std.stdio;
>
> void foo(size_t N)(ubyte[N])
> {
>     writeln("static");
> }
>
> void foo()(ubyte[])
> {
>     writeln("dynamic");
> }
>
> void main()
> {
>     ubyte[16] x;
>     foo(x);
>     foo(x[]);
> }
>
> Up until 2.068.2 (possibly 2.068.0), this printed:
>
> static
> dynamic
>
> Since 2.068.2 it now prints
>
> static
> static
>
> Why? Is there a good reason for this, or should it be a 
> regression? I always thought if you sliced a static array, that 
> became a dynamic array.
>
> Note that doing this:
>
> auto y = x[];
> foo(y);
>
> does print dynamic as I expected.
>
> The context for this is 
> https://issues.dlang.org/show_bug.cgi?id=16519. I wanted to say 
> in there "at least there's a workaround, just slice the input". 
> But I guess that doesn't work!
>
> -Steve

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


More information about the Digitalmars-d mailing list