How to obtain certain traits of delegates returned by functions in template constraints?

Philippe Sigaud philippe.sigaud at gmail.com
Sat Dec 14 23:28:23 PST 2013


I have 3 comments (if you're still reading this thread :) )

First, you could let nextGen determine the return type. Since the
template know that nextGen returns an int delegate(int), there is no
need for the user to provide the 'int' parameter.

So user code could become:

void main(string[] argv)
{
    auto exFunc = &exT!(nextGen).exFunc; // <- here
    exFunc();
}

Second, you could have exT return (become) the function itself, saving
you the trouble to call it with exT!(...).exFunc
In that case, exT could be a template function.

Third, you can also use it directly, like this:

    // First call:
    exT!(nextGen)();

If you still want to name it other wise, you can use and alias:


    // Second call:
    alias myFunc1 = exT!(nextGen);
    myFunc1();


Or use a function pointer, as in your initial code:

    // Third call:
    auto myFunc2 = &exT!(nextGen);
    myFunc2();

Which gives us shorter developer and user code. See here.

http://dpaste.dzfl.pl/63a220cf


More information about the Digitalmars-d-learn mailing list