On Monday, 8 June 2015 at 20:02:11 UTC, Andrei Alexandrescu wrote:
I'm trying to collect together motivating examples and to
figure out the semantics of the feature.
I was just playing around with something and I thought being able
to do a loop over an enum at compile time would be convenient.
The motivation is when you have a function that works a little
differently for arrays than for scalers, but it works the same
way for all sizes of arrays (i.e. 1d/2d/3d all the same).
template GenArrayMathFunction(string function_name, string
array_string)
{
const char[] GenArrayMathFunction =
"real" ~ array_string ~ " " ~ function_name ~"(real" ~
array_string ~ " x) {\n" ~
"\treal" ~ array_string ~" result = x.map!(a => " ~
function_name ~ "(a)).array;\n" ~
"\treturn result;\n" ~
"}";
}
private enum array_dimension
{
Dim1 = "[]",
Dim2 = "[][]",
Dim3 = "[][][]"
}
static foreach(i; array_dimension)
{
mixin(GenArrayMathFunction!(f, i));
}
Basically, the first part generates a string representing a
function that uses map to apply a function to some input. The
type of the input depends on a string representing the kind of
array. The enum represents a string for 1/2/3-dimensional arrays.
Finally, I would want to loop through the enums and use mixin to
create a function for each.
The foreach loop doesn't work right now. Right now, I've just
written out each of the three functions and put in
array_dimension.Dim1, etc.