Static foreach bug?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Sep 3 08:31:37 UTC 2018


n Monday, September 3, 2018 12:39:17 AM MDT Neia Neutuladh via Digitalmars-d 
wrote:
> On Monday, 3 September 2018 at 04:43:30 UTC, bauss wrote:
> > On Sunday, 2 September 2018 at 20:01:08 UTC, Neia Neutuladh
> >
> > wrote:
> >> On Sunday, 2 September 2018 at 19:42:20 UTC, bauss wrote:
> >>> Woud be so much more maintainable if I could have each
> >>> statement into a variable that could be maintained properly.
> >>
> >> You could extract the body of the static foreach into a
> >> [template] function.
> >
> > I'm aware of that, but it's an unnecessary work around for
> > something as trivial as the alternative would have been.
>
> You would need to mark symbols as scoped to the static foreach
> body, or else as exported from a scope to an outer scope. So it's
> not exactly trivial.

Yeah. Having scoping wouldn't work. You would either need a way to name each
of the enums individually (which you can totally do - it's just a bit of a
pain), or you'd need a way to indicate that a particular enum was somehow
scoped to that particular iteration of the loop while the symbol you really
wanted was not restricted to that iteration.

Really, what this comes down to is that static foreach doesn't do anything
special beyond extend what foreach already did when iterating over a
compile-time construct like an AliasSeq. It makes it so that you can iterate
over stuff like arrays at compile-time (instead of just stuff that only
exists at compile-time), and it makes it so that you can use it outside of
functions in order to add declarations, but it works fundamentally the same
way. The only major difference in its semantics is that it does not
introduce a new scope (unlike a foreach over an AliasSeq), because that
doesn't work with declarations, but otherwise, it works basically the same
as a foreach of an AliasSeq. You would need a fundamentally new construct in
order to have something that's somehow tied to a particular iteration of the
loop while still having having declarations that aren't tied to a particular
iteration of the loop. While such a construct would be useful for some uses
of static foreach, it wasn't part of the core concept, and it's far less
obvious what such a construct should look like. Maybe, we'll get such an
improvement at some point, but it's not necessariy for static foreach to do
its core job.

As things stand, if you want to create a symbol specific to a particular
iteration of the loop, you're going to have to use a string mixin to give it
a name unique to that iteration (presumably embedding either the index or
the key into the name).

- Jonathan M Davis





More information about the Digitalmars-d mailing list