On type functions

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue May 5 05:03:21 UTC 2020


On Sunday, 3 May 2020 at 10:52:37 UTC, Stefan Koch wrote:
> If there are any other open questions about how this is 
> supposed to work I am happy to answer them.

This is really cool!

I always thought using recursion is awkward and that we needed 
something better. I posted about it a long time ago [in a galaxy 
not too far away]: 
https://forum.dlang.org/post/mailman.62.1396532327.19942.digitalmars-d@puremagic.com

The linked post pasted verbatim here:

-----
template FilterInts(Args...)
{
     foreach (T; Args)
     {
         static if (is(T == int))
             FilterInts ~= T;  // populate a type tuple
     }
}

void main()
{
     static assert(is(FilterInts!(int, float, int, string) ==
TypeTuple!(int, int)));
}
-----

Or another one:

-----
template GetFirstArray(Args...)
{
     foreach (T; Args)
     {
         static if (isArray!T)
         {
             GetFirstArray = T;
             break;
         }
     }
}

void main()
{
     static assert(is(GetFirstArray!(int, int[], float, float[]) 
== int[]));
}
-----

But I like the explicitness of `alias` for type functions.


More information about the Digitalmars-d mailing list