On 2/5/23 17:20, John Chapman wrote:

> staticMap's "fun" can only be
> instantiated with a single argument, while I need it to work with two.

I adapted staticMap's implementation to two sets of arguments:

import std.meta : AliasSeq;

// The first half of 'args' is the "first arguments" and
// the second half is the "second arguments".
//
// (This can be generalized to N sets of arguments.)
template staticMap2(alias fun, args...)
{
    alias firsts = args[0 .. $ / 2];
    alias seconds = args[$ / 2 .. $];

    static assert(firsts.length == seconds.length,
                  "Mismatched number of first and second arguments");

    alias staticMap2 = AliasSeq!();
    static foreach (i; 0 .. firsts.length) {
        staticMap2 = AliasSeq!(staticMap2, fun!(firsts[i], seconds[i]));
    }
}

// An example struct with two template parameters
struct S(T, size_t length) {
}

// An example template that creates instantiations of the S template
// (This can be generalized to instantiation of any template.)
template Instantiate(T, size_t length) {
    alias Instantiate = S!(T, length);
}

// An example use
alias myTypes = AliasSeq!(int, double, long);
alias mySizes = AliasSeq!(1, 2, 3);
alias result = staticMap2!(Instantiate, myTypes, mySizes);

pragma(msg, result);

void main() {
}

Ali

Reply via email to