www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - assuaging static array literals

I was thinking that two functions may help with static arrays of 
statically-known data:

import std.conv, std.stdio, std.traits;

auto staticArrayRef(Args...)() {
     static string expand() {
         string result = "Args[0]";
         foreach (i; 1 .. Args.length) {
             result ~= ", Args[" ~ to!string(i) ~ "]";
         }
         return result;
     }
     enum size_t len = Args.length;
     static CommonType!(Args)[len] result = mixin("["~expand()~"]");
     return result;
}

void main() {
     auto a = staticArrayRef!(1, 2, 3, 4.5)();
     writeln(typeof(a).stringof);
     writeln(a);
}

Essentially staticArrayRef returns a reference to a 
statically-initialized array with statically-known data.


Andrei
Mar 15 2010