Type programming game

Stefan Koch uplink.coder at googlemail.com
Sun Oct 11 01:07:21 UTC 2020


On Sunday, 11 October 2020 at 00:47:26 UTC, Timon Gehr wrote:
>
> I guess the canonical solution is something like this:
>
> alias Seq(T...)=T;
> template Group(T...){
>     static if(T.length==0) alias Group=Seq!();
>     else static if(T.length==1) alias Group=Seq!(1,T[0]);
>     else{
>         alias R=Group!(T[1..$]);
>         static if(is(T[0]==R[1])) alias 
> Group=Seq!(R[0]+1,R[1..$]);
>         else alias Group=Seq!(1,T[0],R);
>     }
> }
> template solve(T...){
>     template rec(G...){
>         static if(G.length==0) enum rec="";
>         else{
>             enum r=rec!(G[2..$]);
>             enum s=G[1].stringof~(r.length?" ":"")~r;
>             static if(G[0]==2) enum rec="double "~s;
>             else static if(G[0]==3) enum rec="triple "~s;
>             else enum rec=r;
>         }
>     }
>     alias solve=rec!(Group!T);
> }
>
> static assert(solve!(int,int,uint,uint,uint)=="double int 
> triple uint");
> static assert(solve!(char,char,wchar,dchar,dchar)=="double char 
> double dchar");

I am thoroughly impressed.

... also I found a bug in the type-function implementation which 
will integer-promote the type char into int ... and therefore 
fail the testcases I set myself.

The type-function solution will come tomorrow (since I don't post 
code that doesn't actually run).



More information about the Digitalmars-d mailing list