Created attachment 1341
patch against dmd commit 8e2b13b025626127e5e2a0fb90326e655f3deac8
here is the discussion:
http://forum.dlang.org/thread/[email protected]
summary: compiler should accept both foreach (v; smth) and foreach(auto v;
smth) forms. this can be noncense from compiler writer PoV, but ordinary
language user will not be confused by declaring variable without any type.
this change will not break any existing code ('auto' is purely optional).
patch attached (parser fix and two simple test cases).
Comment #1 by ketmar — 2014-03-30T06:59:19Z
ah, forgot to say that i hereby passing this patch to Public Domain (or licensing it under WTFPL if PD is not working in your country).
Comment #2 by andrej.mitrovich — 2014-04-21T19:53:02Z
I don't think this makes any sense. If we were forced to either write auto or ref, then yeah this would actually save us from some bugs, e.g. iterating over a struct array by value (a very common bug IMHO):
-----
struct S { int x; }
void main()
{
S[] arr;
arr.length = 2;
foreach (s; arr)
s.x = 10;
// fails
assert(arr == [S(10), S(10)]);
}
-----
In this case if we were force to use 'auto' or 'ref' then this bug would be easier to catch.
But adding support for 'auto' without requiring a change will do us no good. People will start questioning "why is there an auto if it doesn't do anything", and it's always a bad idea to have two syntaxes that do the exact same thing. It will only cause confusion.
Comment #3 by ketmar — 2014-08-24T15:03:50Z
Created attachment 1387
new foreach-auto patch
2Andrej Mitrovic: i completely agree that 'auto' must be forced, but we can't do this without breaking the code. hence i made 'auto' optional.
and i have a brand new patch, which allows even more useless things! now it allows things like:
foreach (; ...)
foreach (auto; ...)
foreach (n, auto; ...)
Comment #4 by ketmar — 2014-08-24T19:57:19Z
Created attachment 1388
updated patch
patch updated. now things like "foreach (int n; [0, 1, 2, 3]) {}" are allowed.
Comment #5 by ketmar — 2014-08-24T20:11:33Z
i meant "foreach (int; [0, 1, 2, 3]) {}"
Comment #6 by robert.schadek — 2024-12-13T18:18:58Z