Skip to content

Instantly share code, notes, and snippets.

@sinfu
Created October 23, 2010 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinfu/642597 to your computer and use it in GitHub Desktop.
Save sinfu/642597 to your computer and use it in GitHub Desktop.
enum N = 20;
alias Lambda!(int, N, // P = int, q = N
q{
alias P[a][q] _;
})
IntArray;
static assert(is(IntArray!(3) == int[3][N]));
alias Lambda!(3, // p = 3
q{
enum _ = p * a;
})
triple;
static assert(triple!(11) == 33);
//----------------------------------------------------------------------------//
template Lambda(specs...)
{
alias LambdaGen!(specs[ $ - 1],
specs[0 .. $ - 1]).Lambda Lambda;
}
private template LambdaGen(string decl, captures...)
{
template Lambda(args...)
{
alias _!args._ Lambda;
}
template _(args...)
{
mixin injectParameters!(0, +args.length);
mixin injectCaptures !(0, +captures.length);
mixin injectBody;
}
mixin template injectBody()
{
mixin(decl);
}
// Inject named parameter aliases: a-h.
mixin template injectParameters(size_t i, size_t n)
{
static if (i < n && i < 8)
{
mixin injectParameter !(i );
mixin injectParameters!(i + 1, n);
}
}
mixin template injectParameter(size_t i)
{
mixin("alias Id!(args[i]) "~ "abcdefgh"[i] ~","
~ "ABCDEFGH"[i] ~";");
}
// Inject named captures: p-w.
mixin template injectCaptures(size_t i, size_t n)
{
static if (i < n && i < 8)
{
mixin injectCapture !(i );
mixin injectCaptures!(i + 1, n);
}
}
mixin template injectCapture(size_t i)
{
mixin("alias Id!(captures[i]) "~ "pqrstuvw"[i] ~","
~ "PQRSTUVW"[i] ~";");
}
}
template Id( E) { alias E Id; }
template Id(alias E) { alias E Id; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment