www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ParameterIdentifierTuple returns empty strings

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I need to retrieve the parameter identifier but only empty 
strings are returned:
tuple("", "")

``` d
alias fpt = extern(C) nothrow void function(int a, int b);

void main()
{
     import std.traits : ParameterIdentifierTuple;
     pragma(msg, ParameterIdentifierTuple!(fpt));
}
```

Where is here the error?

Kind regards
André
Dec 02 2020
next sibling parent user1234 <user1234 12.de> writes:
On Wednesday, 2 December 2020 at 11:46:26 UTC, Andre Pany wrote:
 Hi,

 I need to retrieve the parameter identifier but only empty 
 strings are returned:
 tuple("", "")

 ``` d
 alias fpt = extern(C) nothrow void function(int a, int b);

 void main()
 {
     import std.traits : ParameterIdentifierTuple;
     pragma(msg, ParameterIdentifierTuple!(fpt));
 }
 ```

 Where is here the error?

 Kind regards
 André
If you look at the template [1] implementation, function pointers are rejected, apparently because of a `__parameters` limitation. There something to report. Bad documentation at least. [1]: https://github.com/dlang/phobos/blob/2c0660141748a13637ff473cbb7b0d52eb1c44db/std/traits.d#L1400
Dec 02 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 2 December 2020 at 11:46:26 UTC, Andre Pany wrote:
 alias fpt = extern(C) nothrow void function(int a, int b);
Function pointers don't really have parameter names. They are allowed for user habit and documentation purposes, but the compiler mostly* ignores them; they aren't actually part of the formal type. * I'm pretty sure I've seen some weird cases where it did work but I can't recall right now. So this alias erases the names before it does anything with them. This is useful, if you did like extern(C) nothrow void foo(int not_a, int not_b); fpt thing = &something; That assignment is still allowed because the names are not considered. Change anything else though and you get a type mismatch. Moreover, consider reflection on that. Are Parameters!thing `a, b` because of the alias, or `not_a, not_b` because of the function assigned? Pretty obvious it can't be the latter, because what the variable actually points to is not known at compile time. But then if it was the former that'd be kinda weird because it wouldn't match what you set. So I think think it is doing the best it can by just not giving the info. Now btw if you do want the names off `something`, ParameterIdentifierTuple!something would give you the not_a, not_b. Maybe you can use that in your code. If you pass a thing as an `alias` template parameter, without the & operator, it retains the names.
Dec 02 2020
parent Andre Pany <andre s-e-a-p.de> writes:
On Wednesday, 2 December 2020 at 15:40:09 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 2 December 2020 at 11:46:26 UTC, Andre Pany wrote:
 [...]
Function pointers don't really have parameter names. They are allowed for user habit and documentation purposes, but the compiler mostly* ignores them; they aren't actually part of the formal type. [...]
Thanks for all the answers, this solves my issue. Kind regards Andre
Dec 02 2020