On Monday, 13 July 2015 at 02:52:11 UTC, Laeeth Isharc wrote:

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.

I had considered mixins as messy, but my original one was way messier than this. By comparison, it's not so bad.

Beyond the mixins, what I find most interesting was the __traits(identifier,functions[0]). Hadn't seen that before. I was only familiar with __FUNCTION__. It definitely simplifies it

Anyway, the downside of the approach so far is that I can't quite understand why some versions have worked, but not others. For your foo version (with a few modifications to better match what I'm doing), I was able to get it working regardless of whether it was in main or not. That's good. Further, when I got rid of the foo, but kept it in main, it still worked (after changing the function names). However, when I moved it back out of main I was getting messages about there being no cos/sin available for those types.

Note: some of the above seemed to only work when I kept the std.math.cos, std.math.sin text in there. When I take it out, I get warnings about recursive aliases. But, I can't seem to use a foreach loop. The recursive mixin template seems to be required.

So this is the last thing I did (that didn't work). Adding foo back to the alias works.

mixin template callThemAll(functions...)
{
   mixin("alias " ~__traits(identifier, functions[0]) ~
" = givemeabettername!(std.math."~__traits(identifier, functions[0]) ~ ");");
    static if(functions.length >1)
                mixin callThemAll!(functions[1..$]);
}

mixin callThemAll!(cos, sin);

Reply via email to