Is old style compile-time foreach redundant?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 8 20:07:53 UTC 2018


On Monday, January 08, 2018 06:27:12 H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Sun, Jan 07, 2018 at 10:39:19PM -0500, Steven Schveighoffer via 
Digitalmars-d-learn wrote:
> > On 1/6/18 6:25 PM, Ali Çehreli wrote:
> > > Is 'static foreach' sufficient for all needs or is there any value
> > > for regular foreach over compile-time sequences?
> >
> > If you use continues or breaks, then you need to switch to gotos if
> > using static foreach, as it does not support them directly.
>
> [...]
>
> Are you sure?  I was under te impression that it does support continues
> and breaks -- but only if they are labelled, because of a syntactic
> ambiguity otherwise.

It does support them if they're labeled. I did it just the other day.
Originally, that code was not using static foreach (though in both cases, it
was over an AliasSeq of template arguments), and I'd used a normal break
(which compiles just fine with a non-static foreach) and been very confused
about why my tests were failing. since for whetever reason, it didn't occur
to me when I was writing it that break would break out of the foreach
(probably since I was thinking of it as a compile-time thing and not as a
loop that would be run, but I don't know; it seems kind of dumb in
retrospect either way). Since my tests failed miserably, I realized
something was wrong and fixed it (though man was it confusing to begin
with), but I was later able to switch that foreach to static foreach without
any breakage. However, if I remove the label now, then it won't compile,
since it's a static foreach. So, if I'd used a static foreach from the
get-go, it would have actually caught a bug for me before I even ran my
tests.

But regardless, labeled break definitely works within a static foreach, and
I expect that a labeled continue does as well, but I haven't tried it.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list