local enum

Mr.Bingo Bingo at Namo.com
Sat Jul 7 21:38:18 UTC 2018


On Saturday, 7 July 2018 at 19:29:02 UTC, Adam D. Ruppe wrote:
> On Saturday, 7 July 2018 at 18:48:35 UTC, Mr.Bingo wrote:
>> The enums are local in nature and are used to store 
>> intermediate results but the looping tries to redefine them, 
>> which gives an error.
>
> Have you tried putting a second set of {} around it?
>
> static foreach(...)
> {{ // 2 intentional
>    enum y = whatever;
> }} // 2 intentional
>
>
> ?

This doesn't always work as it conflates compile time scope and 
runtime scope.

For example, suppose you want to create a series of runtime 
variables:

static foreach(k,...)
{{
     mixin("auto x"~k~" = ...");
}}

You want xk's to escape the foreach but they don't because of the 
inner {}.

You can't move it out and expect things to work:

static foreach(k,...)
{{
     {
         enum y = 3;
     }
     mixin("auto x"~k~" = y");
}

better to achieve a proper solution that works in general.

One doesn't want to write code then later have to modify it and 
then realize it can't be modified properly because of the lack of 
straight forward principles.


More information about the Digitalmars-d mailing list