Annoyance: 'Shadowing declaration is deprecated'+ mixins

Pragma ericanderton at yahoo.removeme.com
Tue Jul 24 11:30:57 PDT 2007


Tristam MacDonald wrote:
> Bill Baxter Wrote:
>> Don Clugston wrote:
>>> Is there any chance of getting the 'shadowing declaration is deprecated' 
>>> error message disabled, for code inserted via a mixin?
>>>
>>> It means that if you inject a 'for' loop into the current function, it 
>>> appears to work, but will fail if another variable with the same name 
>>> already exists.
>>>
>>> void main()
>>> {
>>> // double i=300.0;
>>>    mixin("for (int i=0; i<10; ++i) { func(i); }");
>>> }
>>>
>>> When such code is generated by a mixin, the shadowing does not indicate 
>>> a probable error.
>>>
>>> This is proving to be quite annoying for my BLADE rewrite.
>> I guess one obvious solution is to give your counter variables some 
>> names that are more likely to be unique:
>>
>>      mixin("for (int __i8473=0; __i8473<10; ++__i8473) { func(__i8473); }");
>>
> 
> Hmm... This looks like a prime candidate for something like Scheme's 'gensym' call (which basically alliases an unique, un-named, and un-nameable symbol). Does anyone know of a way to accomplish the same at compile time in D?

FWIW, there was a thread here a while back about generating unique values at compile time.  In short, it's not possible 
(using pure D anyway) to maintain a state extrinsic to a set of template evaluations or CTFE calls.  You need that 
property in order to guarantee uniqueness in your symbols by employing a counter variable to distinguish them.

You have to either adopt some template warts to explicitly pass this state to every template/CTFE or use something 
outside D for all generated symbols to be unique.  BCS (I think) suggested a rather elegant hack that uses a C 
preprocessor to work around this limitation.  Another suggestion was to couple __LINE__ and __FILE__ to get *some* 
measure of uniqueness per-line.  So it can be done, but it won't necessarily be pretty.

It kinda makes me wish D had a __UNIQUE__ symbol (reliably unique for a given module/compilation unit?) for this kind of 
stuff.

const char[] sym = __UNIQUE__;
mixin("for (int " ~ sym ~ "=0; " ~ sym ~ "<10; ++" ~ sym ~ ") { func(" ~ sym ~ "); }");

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list