When is a dynamic array really a static array?
Eugene Wissner
belka at caraus.de
Mon Dec 30 16:14:41 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
It is probably a bug:
You can work around with an assignment:
void main()
{
ubyte[16] x;
foo(x);
auto y = x[];
foo(y);
}
and typeof(x[]) gives you of course ubyte[], not ubyte[16].
More information about the Digitalmars-d
mailing list