On type functions

Max Samukha maxsamukha at gmail.com
Sun May 3 21:14:43 UTC 2020


On Sunday, 3 May 2020 at 09:35:34 UTC, Stefan Koch wrote:

>
> string NameOfField(alias T, size_t fIdx)
> {
>     assert(is(typeof(T.tupleof), T.stringof ~ " has no .tupleof 
> property maybe it's not an aggregate?");
>     return __traits(identifier, T.tupleof[fIdx]);
> }
>
>
> string structToString(T)(T struct_)
> {
>
>     char[] result;
>     result ~= __traits(identifier, T) ~ " :: {\n";
>     foreach(fIdx, field; struct_.tupleof)
>     {
>         result ~= NameOfField!(T, fIdx) ~ " : "; // looks like 
> a templates but it's not!
>
>     }
> }

Cool! Does it mean the following will work?

alias[] map(alias f, alias[] a) {
    alias[] r;
    foreach (e; a)
        r ~= f!e; // can we?
    return r;
}

enum isType(alias a) = is(a);

static assert([map!(isType, AliasSeq!(1, int, "meh"))] == [false, 
true, false]);




More information about the Digitalmars-d mailing list