I don't know what exactly you're after, but you can use
foreach on a whatever-they're-called-now tuple (there's been a
discussion about the name which I haven't followed; I mean the
kind you get from a TemplateTupleParameter):
----
void f1() {}
void f2() {}
void callThemAll(functions ...)()
{
foreach(f; functions) /* The loop is unrolled at compile
time. */
{
f();
}
}
void main()
{
callThemAll!(f1, f2)();
}
----
As usual, recursion is an alternative:
----
void callThemAll(functions ...)()
{
static if(functions.length > 0)
{
functions[0]();
callThemAll!(functions[1 .. $])();
}
}
----
Sorry, I don't think I made myself clear enough. Your code
allows you to do something like call multiple functions in a
loop. I'm talking about the fact that
alias cos = givemeabettername!(std.math.cos);
alias sin = givemeabettername!(std.math.sin);
are just two functions of many in std.math. Suppose I wanted to
write it so that every function in std.math had an array
version generated by this code. I would be repeating this alias
line once each time. My point is that I don't see a way to do
this in a loop. I don't think I can do something like
void callThemAll(functions ...)()
{
foreach(f; functions)
{
alias __Function__ = givemeabettername!(f); //where
__Function__ is the name of the function for f, not callThemAll
}
}
callThemAll(std.math.cos, std.math.sin);
void main()
{
real[] x = [0, PI];
auto y = cos(x);
auto z = sin(x);
}
I am venturing in territory still new to me, but I think that was
his point - foreach with tuples looks like it is looping, but
really it is unpacking them statically at compile time. And
similarly with the recursive version. I don't know if you can
avoid the mixin, but you can make it a little tidier.
import std.math;
import std.stdio;
import std.traits;
mixin template callThemAll(functions...)
{
mixin("alias
foo"~__traits(identifier,functions[0])~"="~__traits(identifier,functions[0])~";");
static if(functions.length >1)
mixin callThemAll!(functions[1..$]);
}
void main()
{
mixin callThemAll!(sin,cos,tan);
writefln("%s",foosin(1));
writefln("%s",foocos(1));
writefln("%s",footan(1.0));
}
Not sure if you knew this already and still found it too messy.