D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4508 - tuples should be indexable with foreach over range
Summary: tuples should be indexable with foreach over range
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-26 09:53 UTC by David Simcha
Modified: 2018-05-17 16:32 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2010-07-26 09:53:12 UTC
The following should really work, as long as the bounds of the foreach loop are known at compile time:

import std.typecons;

void main() {
    auto tup = tuple(1, "foo", 3.0).expand;
    foreach(i; 0..tup.length) {
        auto val = tup[i];
    }
}
Comment 1 bearophile_hugs 2014-10-11 11:21:01 UTC
I think this is a bad idea, in this form. So I suggest to close this issue.

I prefer this syntax much more:

static foreach(i; 0..tup.length) {
Comment 2 Nick Treleaven 2017-04-20 12:11:21 UTC
`foreach (e; seq)` works at compile time for an AliasSeq, so I don't see why `foreach (i; low..high)` can't make `i` known at compile-time if the bounds are known.

I think we should rename this bug to apply to any CT-knowable index in a ForeachRangeStatement.
Comment 3 ag0aep6g 2017-04-20 16:10:55 UTC
(In reply to Nick Treleaven from comment #2)
> `foreach (e; seq)` works at compile time for an AliasSeq, so I don't see why
> `foreach (i; low..high)` can't make `i` known at compile-time if the bounds
> are known.

Can `i` be made known at compile-time without unrolling the loop (in the binary)? Surely we don't want to unroll every loop that can possibly be unrolled. For example, it would be very surprising if the compiler unrolled a loop like `foreach (i; 0 .. uint.max) writeln(i);`, generating gigabytes of machine code.
Comment 4 Nick Treleaven 2017-04-22 07:52:54 UTC
In that case, perhaps we could use `static foreach` as in comment 1.
Comment 5 Dmitry Olshansky 2018-05-17 16:32:04 UTC
With static foreach you can do this, and AFAICT is exactly what is required:

import std.typecons;

void main() {
    auto tup = tuple(1, "foo", 3.0).expand;
    static foreach(i; 0..tup.length) {{
        auto val = tup[i];
    }}
}