Suggestion for a new trait: __traits(getCaptures,...)

rikki cattermole rikki at cattermole.co.nz
Wed Oct 23 02:37:27 UTC 2019


On 23/10/2019 5:42 AM, Paul Backus wrote:
> On Tuesday, 22 October 2019 at 14:26:49 UTC, Zoadian wrote:
>> Example how i would imagine it:
>> ```
>> struct S{ int b; }
>> int a;
>> S s;
>>
>> auto fun = (){ return a + s.b; }
>>
>> alias vars = __traits(getCaptures, fun);
>>
>> assert_alias(vars[0] == a, vars[1] == s.b);
>> ```
> 
> Rather than a new `__traits`, it would also suffice to add a `.captures` 
> property to delegates. Then we could write the following:
> 
> int a;
> auto fun = () { return a; };
> assert(__traits(isSame, a, fun.captures[0]));

This would require a DIP, adding a trait however would not.


More information about the Digitalmars-d mailing list