Thread overview |
---|
November 07, 2013 std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
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? |
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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 |
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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? |
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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 |
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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. |
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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
}
|
November 07, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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 |
November 09, 2013 Re: std.traits.ParameterIdentifierTuple bug or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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. |
Copyright © 1999-2021 by the D Language Foundation