On Monday, 25 February 2019 at 06:51:20 UTC, Yevano wrote:
This only works for at most 3 parameter delegates. If I want to add more, I have to linearly add more static ifs in the obvious way. However, I believe I can make this function scalable using string mixins and other magic. Any insight into this is much appreciated.

The simple scalable version - just change maxArgs to a number that suits you:

Abstraction L(alias f)() {
    import std.meta : Repeat;
    enum maxArgs = 20;
    static foreach (i; 0..maxArgs) {
        static if (__traits(compiles, f(Repeat!(i, null)))) {
            Repeat!(i, Variable) vars;
            foreach (ref e; vars) {
                e = new Variable();
            }
            auto result = new Abstraction(vars[0], f(vars));
            foreach (e; vars[1..$]) {
                result = new Abstraction(e, result);
            }
            return result;
        }
    }
}

The problem with lambdas is they only present an opaque name when inspected with .stringof, so you can't easily check the arity. For regular templated functions it can be done, but requires parsing some substantial subset of D code.

--
  Simen

Reply via email to