[Issue 17771] foreach over const input range fails

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Aug 21 23:20:01 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17771

Eyal <eyal at weka.io> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eyal at weka.io

--- Comment #4 from Eyal <eyal at weka.io> ---
Leaving the "const" there (for input ranges) will cause compile-errors
(popFront generally requires mutability).

Removing the attribute will sometimes fail.
Keeping the attribute will always fail.

For the cases where it need not fail - why fail?

It may not be consistent with other behaviors - but there is no case for which
it has worse behavior. It is much more useful.

D has many inconsistencies for usefulness:

* Pointers are dereferenced in some contexts but not others
* Integers have valid init values, floats have invalid ones
* Arrays and slices have specific foreach behavior and not the input-range
behavior
* Tuples have special handling in the language

Shouldn't ability to iterate a const/immutable range be important enough to
warrant a slight inconsistency?

foreach can simply be defined as calling unqual on its argument.

--


More information about the Digitalmars-d-bugs mailing list