Get vars in current scope at compile time?

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 20 02:36:19 PST 2014


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.


More information about the Digitalmars-d-learn mailing list