static foreach issues
Psychological Cleanup via Digitalmars-d
digitalmars-d at puremagic.com
Tue Sep 5 22:48:43 PDT 2017
On Wednesday, 6 September 2017 at 05:23:47 UTC, Ali Çehreli wrote:
> On 09/05/2017 07:05 PM, Psychological Cleanup wrote:
> > Nesting static foreach and using enum has latent problem.
> >
> > static foreach()
> > static foreach()
> > {
> > enum A = ;
> > }
> >
> > compiler complains because A is defined multiple times by the
> outer most
> > foreach.
>
> That's understandable as there are multiple As dropped into
> code.
>
> > To fix this
> >
> > static foreach()
> > static foreach()
> > {
> > {
> > enum A = ;
> > }
> > }
> >
> > But then compiler complains about unexpected { if in a scope
> where
> > normal {}'s are not expected.
>
> Can you demonstrate with compilable code? Doing what you
> describe works for me:
>
> void main() {
> static foreach(i; 0 .. 3)
> static foreach(j; 0 .. 3)
> {
> {
> enum A = i * j;
> }
> }
> }
>
> Ali
Yes, but I said you can't do that. Put the static foreaches
outside main in to a block where one can't simply do {} and it be
valid.
You see, the point is that
static foreach
static foreach
{
}
works at the module level
static foreach
static foreach
{
enum a = ;
}
fails because of multiple a's
so
static foreach
static foreach
{
{
enum a = ;
}
}
to solve that(which, in your given example, works inside main)
but fails at the module scope because the { }'s we used to solve
the first problem created a new problem. The two methods are
mutually exclusive but both are required.
Sure, I could wrap stuff around a function, but what if I'm using
them for code generation inside a class? I have to use the extra
{ } to prevent the enum error but then I get a new error about
the { }.
class X
{
static foreach
static foreach
{
// fails, multiple a
enum a = ; // part of class generation code like
generating functions
}
}
class X
{
static foreach
static foreach
{
// fails, { } block inside a class.
{
enum a = ; // part of class generation code like
generating functions
}
}
}
it works fine if we use it in a function because we can have { }
in functions without problems.
Do you not see the contradictory nature? { } required but { }
forbidden.
More information about the Digitalmars-d
mailing list