On Friday, 25 November 2016 at 14:51:52 UTC, Jonathan M Davis wrote:
I think you may write it (I mean actual D) with using some
template like this:

auto array = static_array!uint(1, 2, 3, 4)

Could you please help to write down this template in the best and clear manner?

That's easy. The problem is if you want it to have the same semantics as

uint[4] arr = [1, 2, 3, 4];

In particular, VRP (Value Range Propagation) is a problem. This compiles

ubyte[4] arr = [1, 2, 3, 4];

because each of the arguments is known to fit in a ubyte. However, making

auto arr = staticArray!ubyte(1, 2, 3, 4);

do the same without forcing a cast is difficult. And if you force the cast, then it's not equivalent anymore, because something like

ubyte[4] arr = [1, 2, 3, 900];

would not compile. And surprisingly, having the function take a dynamic array doesn't fix that problem (though maybe that's something that we could talk the dmd devs into improving). e.g.
To mine mind it is not a problem because when you write you think what you write (or you are right). Morover compiler will always tell you are wrong if you make 256 to be ubyte implicity.


auto arr = staticArray!ubyte([1, 2, 3, 4]);

doesn't compile either. The most straightforward implementations are something like
Why? Is it since [1, 2, 3, 4] is int[4] by default?


T[n] staticArray(T, size_t n)(auto ref T[n] arr)
{
    return arr;
}

or

auto staticArray(Args...)(Args args)
{
    CommonType!Args[Args.length] arr = [args];
    return arr;
}
Great! Thank you. I should take more precise look at std.traits


but I don't know if the VRP problem is solvable or not without some compiler improvements. If there's a clever enough implementation to get VRP with a function like this with the current language, I haven't figured it out yet.

- Jonathan M Davis
As I noticed, to my mind it is not a hindrance to write deliberate things.

- Igor Shirkalin

Reply via email to