the best language I have ever met(?)

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 25 08:30:53 PST 2016


On Friday, November 25, 2016 17:03:32 Artur Skawina via Digitalmars-d-learn 
wrote:
> On 11/25/16 15:51, Jonathan M Davis via Digitalmars-d-learn wrote:
> > On Friday, November 25, 2016 14:27:39 Igor Shirkalin via
> > Digitalmars-d-learn>
> > 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. [...]
>
>    enum T[N] staticArray(T, alias ELS, size_t N=ELS.length) = ELS;
>    auto arr = staticArray!(ubyte, [1, 2, 3, 4]);

That won't work with variables. e.g.

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

would fail to compile. It only works when all of the values are known at
compile time, whereas

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

would compile just fine.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list