Accessing all data in TypeTupple (AliasSeq) and stringify them

Voitech via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 25 12:53:12 PST 2016


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:
>> Hi, I have some code processing functions definition in 
>> compile time, I want to override
>> them in some other class but not explicitly so created this 
>> code:
>>
>> template MixinFunction(alias attributes,alias returnType,alias 
>> name,alias parameters,alias bodyy){
>>
>> 	enum string MixinFunction = format(q{
>> 			%s %s %s(%s){
>>
>> 				%s
>>
>> 			}
>>
>> 		},attributes,returnType,name,parameters,bodyy);
>> }
>> unittest{
>> 	alias func=MixinFunction!("static","void","testFunc","int 
>> a,int b",q{
>>
>> 			import std.stdio;
>> 			writeln("im void body");
>>
>> 		});
>> 	pragma(msg,func);
>> 	mixin(func);
>> }
>>
>> Now i acquired all data like return type, parameters but need 
>> to turn them into string for example function parameters must 
>> have form of:
>>
>> for function func:
>> void func(int a,string b){}
>>
>> alias parameters=Parameters!func;
>>
>> returns me AliasSeq!(int,string)
>> I want to to turn them into string: like "int param0, string 
>> param1", but in template
>>
>>     template AliasSeqToString(TList...){
>>
>>         enum AliasSeqToString= ... //each ?
>>
>>
>>     }
>> How to operate on TypeTuple/AliasSeq elemens withouth foreach ?
>
> 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



More information about the Digitalmars-d-learn mailing list