Is old style compile-time foreach redundant?
Simen Kjærås
simen.kjaras at gmail.com
Sun Jan 7 00:41:48 UTC 2018
On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli wrote:
> Is 'static foreach' sufficient for all needs or is there any
> value for regular foreach over compile-time sequences?
There is, as far as I've seen, there's nothing old
foreach-over-tuple can do that new-style static foreach can't.
The big difference is static foreach doesn't introduce a scope,
which is great when that's what you want, and only requires an
additional set of braces otherwise.
In some cases, especially when combined with mixins, this new
behavior might manifest bugs at some later point. Simplified
example:
mixin template declareInts(names...) {
static foreach (name; names) {
mixin("int "~name~";");
}
}
As long as you use it with non-duplicate names, this will work
great. It will fail the moment a junior programmer decides he
wants two ints with the same name. Real-world examples will of
course be more involved.
Old foreach is however not going away any time soon - there's far
too many codebases out there that use the feature.
--
Simen
More information about the Digitalmars-d-learn
mailing list