Accessing all data in TypeTupple (AliasSeq) and stringify them

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 15:36:47 PST 2016


On Thursday, 25 February 2016 at 20:53:12 UTC, Voitech wrote:
> On Thursday, 25 February 2016 at 14:29:30 UTC, Nicholas Wilson 
> wrote:
>> On Thursday, 25 February 2016 at 13:16:43 UTC, Voitech wrote:
>>> [...]
>>
>> You can (see std.meta/(std.traits?) , with recursive 
>> templates), but there is nothing stopping from using 
>> for/foreach in a template
>>
>> this should do what you want
>> string[] functionSig;
>> string[] params;
>> foreach(s; Parameters!T)) // returns AliasSeq of types
>>  {
>>        params ~=s.stringof;
>>  }
>>   string[] pits;
>>  foreach(p; ParameterIdentifierTuple!(T)); // returns AliasSeq 
>> of strings
>> {
>>            pits ~=p;
>> }
>> and the either join(er) or do as you see fit.
>> or use plain old for
>> for(auto i=0;i< pits.length; i++)
>> {
>>      functionSig ~= params[i];
>>      functionSig ~= pits[i];
>> }
>> writeln(functionSig);
>> // should print ["int" , "param0" , "string" , "param1"]
>>
>> Nic
>
> Thank You for answering, well i wanted to make all of this in 
> template block, not using template functions (testing if it is 
> possible), but i'm getting error when i try to create something 
> like
>
> template TupleToString(TList...){
>
>     string a;
>     foreach(T;TList){ // Error: declaration expected, not 
> 'foreach'
>         a~=T.stringof;
>     }
>     enum string TupleToString=a;
> }
>
> Of course i can use template function, but wanted to know if 
> can omit this.
> Cheers Voitech

See the recursive templates in std.meta;

this would be something like

  template TupleToString(TList...)
{
        static if(Tlist.length == 0)
               enum string TupleToString = "";
        else
              enum string TupleToString=TList[0].stringof ~ 
TupleToString(TList[1 . $];
  }

Nic


More information about the Digitalmars-d-learn mailing list