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

Max Haughton maxhaton at gmail.com
Tue Oct 22 14:37:45 UTC 2019


On Tuesday, 22 October 2019 at 14:26:49 UTC, Zoadian wrote:
> Hello!
>
> I tried to implement a reactivity pattern similar to vue.js and 
> ran into the problem that it currently is not possible (correct 
> me if I am wrong) to find out which variables are implicitly 
> captured or accessed in lambdas/member functions.
> Would it be possible to add this to D? `Would it require a DIP?
>
> 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);
> ```
>
> I have a quick and dirty implementation here (it's not good, 
> just POC): 
> https://github.com/Zoadian/dmd/commit/5a4f2f2705d491aa4ce0ae2ea7721f9340cb3d7f
>
> Greetings
> Zoadian

I'd never thought about that one before, good idea.

We need a much wider and deeper traits system.



More information about the Digitalmars-d mailing list