www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.traits.ParameterIdentifierTuple bug or not?

reply "Uranuz" <neuranuz gmail.com> writes:
In my programme I need to get names of function parameters when I 
pass some function reference to template function that does some 
manipulations with it. I have updated my copy of dmd to 2.064, 
because there was a bug in previous versions.
http://d.puremagic.com/issues/show_bug.cgi?id=9967

But I still can't implement the stuff that I wanted. I don't know 
is it bug or not, so I asking for help. I'm trying to do 
something like this:
//-------------------
module parameter_identifier_test;

import std.stdio, std.string, std.conv, std.traits, std.typecons;

void someFunc(int[] aaa, string[string] bbb, double ccc)
{
	
	writeln("Some function executed!!!");
}

void test(FunctionT)(FunctionT func)
{
	alias ParameterIdentifierTuple!(FunctionT) ParamNames;
	pragma(msg, ParamNames);  //Gives compilation output: tuple("", 
"", "")
	writeln(ParamNames);  //Gives empty string
}


void main()
{	
	test(&someFunc);
	
}
//------------------------

Live demo is available at: http://dpaste.dzfl.pl/32300c82

Is there some other way to get names of parameters in situation 
like this?
Nov 07 2013
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 7 November 2013 at 18:19:57 UTC, Uranuz wrote:
 void test(FunctionT)(FunctionT func)
 {
 	alias ParameterIdentifierTuple!(FunctionT) ParamNames;
 	pragma(msg, ParamNames);  //Gives compilation output: 
 tuple("", "", "")
 	writeln(ParamNames);  //Gives empty string
 }


 void main()
 {	
 	test(&someFunc);
 	
 }
 //------------------------

 Live demo is available at: http://dpaste.dzfl.pl/32300c82

 Is there some other way to get names of parameters in situation 
 like this?
You need to use template alias parameter: http://dpaste.dzfl.pl/92de7a5a Function types are decouples from their declarations and this can't provide metainformation such as parameter names or attached UDA's
Nov 07 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
Thanks a lot! Seems that I still don't understand where and for 
what alias is needed and this is may be why I avoid of it's usage 
in template parameters. Can you clear for me base cases where 
alias is essential in template parameters?
Nov 07 2013
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 7 November 2013 at 18:39:44 UTC, Uranuz wrote:
 Thanks a lot! Seems that I still don't understand where and for 
 what alias is needed and this is may be why I avoid of it's 
 usage in template parameters. Can you clear for me base cases 
 where alias is essential in template parameters?
Refer to complete list of valid template parameters : http://dlang.org/template.html#TemplateParameterList (including http://dlang.org/template.html#TemplateAliasParameter) Point of template alias parameter is to capture references to symbols (declarations). For example in this example: ``` void foo1(double param) {} void foo2(double param) {} ``` ..both functions have the same type (http://dlang.org/template.html#TemplateTypeParameter), but are two different and distinct symbols. You use template alias parameter when you need to inspect some property of a symbol - name, parameter names, attached attributes, parent module etc. Also template alias parameters, contrary to normal aliases, do accept literals, including lambda literals. This is extensively used in std.algorithm, you can refer to its source code for examples. Ali has pretty much covered it in his book in "more templates" chapter : http://ddili.org/ders/d.en/templates_more.html
Nov 07 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
So when using alias I'll get much more compile-time information 
than using plain template parameter, where I can pass only type 
or a value.
Nov 07 2013
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 7 November 2013 at 20:01:14 UTC, Uranuz wrote:
 plain template parameter, where I can pass only type or a value.
Only type. Template value parameters are 3d separate case having own entry in linked specification ;) However, alias parameters don't have Implicit Function Template Instantiation and always need to be mentioned explicitly: ``` void foo() {} void bar(T)(T param) { } // legal // void baz(alias T)(T param) { } // illegal void baz(alias T)() { } // legal void main() { bar!(typeof(&foo))(&foo); // legal bar(&foo); // legal // baz(&foo); // illegal baz!foo(); // legal }
Nov 07 2013
parent "Uranuz" <neuranuz gmail.com> writes:
Thanks for explanation. I've tried to get alias template argument 
implicitly, but got compilation error. I will use name of the 
function as template argument, but not reference to function as 
regular parameter.
Nov 09 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-11-07 19:39, Uranuz wrote:
 Thanks a lot! Seems that I still don't understand where and for what
 alias is needed and this is may be why I avoid of it's usage in template
 parameters. Can you clear for me base cases where alias is essential in
 template parameters?
In short, an alias parameter is pass-by-name. -- /Jacob Carlborg
Nov 07 2013