Type programming game

Stefan Koch uplink.coder at googlemail.com
Tue Oct 13 11:33:37 UTC 2020


On Monday, 12 October 2020 at 14:04:56 UTC, claptrap wrote:
> On Sunday, 11 October 2020 at 10:46:00 UTC, Stefan Koch wrote:
>> On Sunday, 11 October 2020 at 09:31:43 UTC, Basile B. wrote:
>>> On Saturday, 10 October 2020 at 23:42:46 UTC, Stefan Koch 
>>> wrote:
>>>> Hi,
>>>>
>> static assert(double_triple(int,int,uint,uint,uint) == "double 
>> int triple uint");
>> static assert(double_triple(char,char,wchar,dchar,dchar) == 
>> "double char double dchar");
>> static assert(double_triple(double, double, float, float, 
>> float) == "double double triple float");
>> ---
>>
>> In continuation I will post benchmarks of all 4 (or more of 
>> others come in) solutions on longer type sequences.
>
> 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;
> }

Thanks for you submission.
You helped me to find a bug within the TF implementation.

For some reason indexing of types[] that does not happen 
automatically in the foreach doesn't work.
(Which is really strange because under the hood it should use the 
same code...)

There is one mistake in your code which I would like to correct.
is-expressions cannot use !=
you have to state it as:
> (!is(types[i] ==  types[i+1]))

Other than that the code should run well, as soon as the bug with 
the indexing of __type arrays is fixed.


More information about the Digitalmars-d mailing list