static foreach and new identifier names

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jan 5 21:30:25 UTC 2018


On Friday, January 05, 2018 18:33:52 Adam D. Ruppe via Digitalmars-d wrote:
> On Friday, 5 January 2018 at 18:07:54 UTC, H. S. Teoh wrote:
> > Simple solution: add another pair of {} to create a nested
>
> > scope:
> That's illegal outside of functions, though, which is the place
> static foreach is special!
>
> Also, introducing a new scope means you can't expose that stuff
> to the outside world, which defeats the point. (I had tried doing
> that and using the alias outside but still fails.) Note that my
> declarations here are NOT temporary - the reason I'm trying to
> use static foreach is to introduce new public members to the
> scope.

In the few cases that I've needed to do something like that, I've ended up
using string mixins for the names. I've only ever needed anything like that
for unit tests though. The few times that I've used static foreach outside
of a function has been inside a template where I inserted static assertions
for each type in a variadic template, which was faster than writing a
template to use with allSatisfy.

But even with a language feature for handling the naming, I don't know how
you'd cleanly deal with any of that if you then need to actually refer to
those variables elsewhere. But clearly, you've come up with a use case that
I've never had. Most of my uses of foreach with AliasSeq (and now using
static foreach) have simply been for generating unit tests. It's pretty rare
that I've even used them inside a normal function let alone wanted to create
anything outside of a function.

- Jonathan M Davis



More information about the Digitalmars-d mailing list