Bug 5435 – Static foreach over tuple ignores type annotation
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-01-08T18:29:00Z
Last change time
2012-06-02T20:51:24Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
necroment
Comments
Comment #0 by necroment — 2011-01-08T18:29:56Z
import std.stdio;
template Tuple(E...) { alias E Tuple; }
enum Type { A, B, C };
alias Tuple!(Type.A, Type.B, Type.C, "foo", 3.0) tuple;
void main() {
foreach (Type foo; tuple)
writeln(foo);
foreach (string foo; tuple)
writeln(foo);
foreach (int foo; tuple)
writeln(foo);
}
---------
This code compiles and runs without errors, printing every element of tuple.
Should the compiler reject static foreach with explicit type annotations ?
Comment #1 by bugzilla — 2011-01-08T21:19:31Z
This behavior is as intended. Not a bug.
Comment #2 by braddr — 2011-01-08T21:24:51Z
I think you ought to rethink your intention then. Allowing a specific type but ignoring it is odd. I think static foreach should require type deduction and error on anything else.
Comment #3 by braddr — 2011-01-08T23:27:48Z
Reopening. To be clear, I think the only two valid syntax's should be:
foreach(t; tuple) { ... }
or
foreach(auto t; tuple) { ... }
I could be convinced that specifying a type is ok, but only for the case of a tuple where all the values match the specified type.