[Issue 17771] New: foreach over const input range fails
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Mon Aug 21 06:00:22 PDT 2017
https://issues.dlang.org/show_bug.cgi?id=17771
Issue ID: 17771
Summary: foreach over const input range fails
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: alex.goltman at gmail.com
```
static struct NumRange {
int front = 0;
int end = 0;
@property auto length() const { return end - front; }
@property bool empty() const { return length == 0; }
void popFront() { front++; }
}
const r = NumRange(0, 5);
foreach (x; r) {
writeln(x);
}
```
results in
```
Error: mutable method test.main.NumRange.popFront is not callable using a const
object
```
even though foreach copies the input range it gets (as evident from running
foreach on same range twice), so it can at least try to copy it to an
non-const.
--
More information about the Digitalmars-d-bugs
mailing list