Function parameters UDAs

ag0aep6g anonymous at example.com
Mon Jun 10 23:35:12 UTC 2019


On 11.06.19 01:12, Johannes Loher wrote:
> I would like to iterate over all parameters of a function using static 
> foreach and then process each parameter's UDAs. But by using static 
> foreach on the parameter tuple, slicing it is not possible anymore, so 
> the suggested workaround does not work in this case :(

The workaround is to work with 1-length slices of your parameter 
sequence instead of elements. You can still do that. You just can't 
`foreach` over the parameters directly. Instead, iterate with an index 
and use it to get slices:

-----
import std;

struct Test
{}

void foo(@(1) Test x, @(2) @(3) float y)
{
}

void main()
{
     alias Params = Parameters!foo;
     pragma(msg, Params);
     static foreach(i; 0 .. Params.length)
     {{
         alias P = Params[i .. i + 1];
         pragma(msg, P);
         static foreach(uda; __traits(getAttributes, P))
         {
             pragma(msg, uda);
         }
     }}
}
----

Prints:

----
(@(1) Test, @(tuple(2), tuple(3)) float)
(@(1) Test)
1
(@(tuple(2), tuple(3)) float)
2
3
----


More information about the Digitalmars-d-learn mailing list