Bug 21746 – Discrepancy between foreach over tuple and static foreach

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-03-22T07:54:34Z
Last change time
2021-03-25T04:22:24Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Mathias LANG
See also
https://issues.dlang.org/show_bug.cgi?id=14831

Comments

Comment #0 by pro.mathias.lang — 2021-03-22T07:54:34Z
``` import std.meta; import std.stdio; void main () { foreach (idx, T; AliasSeq!(int, char, bool, int, string, int, ubyte)) { void checkValue (TT) (TT value, size_t id) { assert(id == idx); assert(value == TT.init); } checkValue(T.init, idx); } writeln("Yep"); } ``` Since the fix for https://issues.dlang.org/show_bug.cgi?id=14831 this runs correctly. However trying to make the `foreach` static will lead to the following errors: ``` foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here foo.d(8): Error: declaration `checkValue(TT)(TT value, size_t id)` is already defined foo.d(8): `template` `checkValue(TT)(TT value, size_t id)` is defined here ``` Obviously this is an unwarranted limitation.
Comment #1 by moonlightsentinel — 2021-03-22T09:58:17Z
Ins't that expected behavior? `static foreach` explicitly does not create a new scope while "normal" foreach does.
Comment #2 by pro.mathias.lang — 2021-03-25T04:22:24Z
You're correct. I guess I have to get used to `static foreach (...) {{ /* CODE GOES HERE */ }}`.