runtime static arrays

Ali Çehreli acehreli at yahoo.com
Tue Nov 20 14:52:56 PST 2012


On 11/20/2012 02:46 PM, Namespace wrote:
> Something like:
> scope int[8] arr;
> or
> scope int[i] arr;
>
> would be cool. You get an resizeable array which capactiy is all the
> time equal to his length. And it is destroyed after the liftetime of the
> scope.
>
> Maybe some stuff for Remus...

This is a surprisingly promising start: :)

import std.stdio;

struct FixedArray(T, size_t N)
{
     T[N] elements;
     alias elements this;
}

void main()
{
     FixedArray!(int, 10) a;

     foreach (int i, ref e; a) {
         e = 42 + i;
     }

     writeln(a);
}

Ali


More information about the Digitalmars-d-learn mailing list