Type programming game

Stefan Koch uplink.coder at googlemail.com
Tue Oct 13 14:43:23 UTC 2020


On Monday, 12 October 2020 at 14:04:56 UTC, claptrap wrote:
>
> Maybe this type function version will work
>
> string double_triple(type[] types...)
> {
>     string result;
>     uint subidx = 0;
>
>     foreach(i, t; types)
>     {
>         if ((i == types.length-1) ||  is(types[i] != 
> types[i+1]))
>         {
>             result ~= 
> ["","double","triple","quadruple"][i-subidx]
>                ~ __traits(identifier, t) ~ " ";
>             subidx = i+1;
>         }
>     }
>     return result;
> }

I fixed your version to work around the bugs in type functions.
(As of git commit 1652c194870f9c66161888425a9cdf01db8187f6 (in 
https://github.com/UplinkCoder/dmd/tree/talias_master))

Here is it fixed:
---

alias type = __type;
string double_triple(type[] types...)
{
     string result;
     size_t subidx = 0;

     foreach(i, t; types)
     {
         // for some reason using type[idx] will only work within 
an assign exp
         // this still to be fixed
         auto t2 = (i == types.length-1) ? cast(type)type.init : 
types[i+1];
         if (!is(t2 == t)) // this works because if t2 is 
type.init it will eval to false;
         {
             if (i-subidx)
             {
                 result ~= 
["","double","triple","quadruple"][i-subidx]
                     ~ " " ~ __traits(identifier, t) ~ " ";
             }

            subidx = i+1;
         }
     }
     return result[0 .. $-1];
}

static assert(double_triple(char,char,wchar,dchar,dchar) == 
"double char double dchar");
static assert(double_triple(char,char,wchar,dchar,dchar, dchar) 
== "double char triple dchar");
---



More information about the Digitalmars-d mailing list