If I slice a static array, the type should be a dynamic array:
int[16] x;
pragma(msg, typeof(x[])); // int[]
auto y = x[];
pragma(msg, typeof(y)); // int[]
However, if you slice a static array, and call a template or regular function which accepts a static array that matches the slice size, for some reason the compiler turns it into a static array again:
import std.stdio;
void foo(size_t N)(int[N] arr)
{
writeln("static: ", N);
}
void foo()(int[] arr)
{
writeln("dynamic");
}
void main()
{
int[16] x;
foo(x); // static 16
foo(x[]); // static 16
foo(x[0 .. 5]); // static 5 (!)
auto y = x[];
foo(y); // dynamic
}
Note the only way to solve this is to create a new variable. The resulting output should be:
static 16
dynamic
dynamic
dynamic
The end result means workarounds for ensuring you pass a slice and not the entire static array can be overridden by the compiler. This is related to issue 16519, as there's no easy workaround.
run.dlang.io seems to suggest it happened in 2.068.2, someone on the forums suggested PR https://github.com/dlang/dmd/pull/4779
Conversation on forums: https://forum.dlang.org/post/[email protected]
Comment #1 by moonlightsentinel — 2019-12-30T18:49:22Z
Digger blames https://github.com/dlang/dmd/pull/4779 when using the following test case:
void foo(size_t N)(ubyte[N], bool sa)
{
assert(sa);
}
void foo()(ubyte[], bool sa)
{
assert(!sa);
}
void main()
{
ubyte[16] x;
foo(x, true);
foo(x[], false);
}