void main()
{
foo1([1, 2, 3, 4]);
foo2([1, 2, 3, 4]);
foo3!ubyte([1, 2, 3, 4]);
foo4!(ubyte, 4)([1, 2, 3, 4]);
foo4!ubyte([1, 2, 3, 4]);
}
void foo1(ubyte[] arr)
{
}
void foo2(ubyte[4] arr)
{
}
void foo3(T)(T[] arr)
{
}
void foo4(T, size_t n)(auto ref T[n] arr)
{
}
The first four calls compile just fine. The fifth one does not. I don't see any reason why it shouldn't be able to work given that the others do, and it's inconsistent that it doesn't. It also makes it impossible for a function like
T[n] staticArray(T, size_t n)(auto ref T[n] arr)
{
return arr;
}
to use VRP and compile with
auto sa = staticArray!ubyte([1, 2, 3, 4]);
which means that it's not possible to duplicate the semantics of
ubyte[4] sa = [1, 2, 3, 4];
with such a function. Obviously, it can be gotten around via casting, but the direct initialization doesn't require it, and it's not required in the first four function calls in the example above - just the one where the size is inferred. So, if it were fixed so that the fifth call were consistent with the other four, then a function like the staticArray function here could duplicate the semantics of the direct initialization (aside from the fact that it inferred the size, which is the point of using the function rather than just initializing the variable with the literal).
Comment #1 by timothee.cour2 — 2018-02-15T02:54:36Z
same problem with tuples
U[T.length] staticArray(U = CommonType!T, T...)(T a)
{
return [a];
}
auto a = staticArray2!bute(1, 2);
Error: cannot implicitly convert expression [_param_0, _param_1] of type int[] to byte[2]
Comment #2 by robert.schadek — 2024-12-13T18:50:48Z