www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get vars in current scope at compile time?

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
Is there some way to get a list of the variables that are in the 
current scope via traits? Some things like allMembers?

I would like to be able to call this from a mixin to grab the 
locals in the scope that the mixin is being dropped into.
Dec 19 2014
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 20/12/2014 10:09 a.m., Tofu Ninja wrote:
 Is there some way to get a list of the variables that are in the current
 scope via traits? Some things like allMembers?

 I would like to be able to call this from a mixin to grab the locals in
 the scope that the mixin is being dropped into.
No way to do this.
Dec 19 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Rikki Cattermole:

 No way to do this.
But perhaps it's worth supporting as future enhancement with a __traits. What are the use cases? Bye, bearophile
Dec 20 2014
parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 20/12/2014 11:03 p.m., bearophile wrote:
 Rikki Cattermole:

 No way to do this.
But perhaps it's worth supporting as future enhancement with a __traits. What are the use cases? Bye, bearophile
Short answer, I'm not keen on the idea, at least not yet. I would far more comfortable once I've categorized the different types of CTFE implements there can be. Since nobody has really done this before. What makes me more uncomfortable to is DIP50. Looking over it again, this is getting rather close to macro systems in LISP family of language who do have full AST access/modification capabilities. So in other words, its just asking for trouble without some proper thought. Long answer, lets consider this code: void myfunc() { int x, y, z; myvalues(); } void myvalues(string file = __FILE__, string func = __FUNCTION__, int line = __LINE__)() { __traits(functionValues, file, func, line, "z") = __traits(functionValues, file, func, line, "x") + __traits(functionValues, file, func, line, "y") } This was meant to be a little off from what was asked. Because I know this will be wanted eventually. Its nasty ambiguous code. A better way might be to pass in a super function state e.g. void myvalues(SSTATET)(SSTATET SSTATE = __SUPERSTATE__) { with(SSTATE) { z = x + y; } } Same idea, but could be used for e.g. mixin templates and what have you. Want all members of SSTATE? __traits(allMembers to the rescue! Either way, not really D.learn material.
Dec 20 2014