[Issue 17771] foreach over const input range fails

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Aug 22 14:40:47 PDT 2017


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

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #12 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
For const to work with ranges, we'd really need some sort of tail-const slicing
mechanism similar to what happens with dynamic arrays and IFTI (though
presumably, it would require help from the programmer as opposed to being
automatic, since it really can't be done automatically with ranges like it can
be with arrays). But wthout some form of tail-const mechanism, it's a waste of
time to muck around with const and ranges. Best case, you make certain use
cases work while having a bunch that don't. At this point, it's far better to
just never use const with ranges. They're pretty much inherently incompatible
given the fact that iterating a range modifies it, and the only defined way to
copy a range is save. Technically, even using a range after passing it to a
function or using foreach on it is undefined behavior in generic code, since
the copy semantics differ wildly depending on how the range is defined. So,
trying to do anything with const automatically is a total minefield. My answer
to anyone trying to use const with ranges is to just give up now. They simply
don't work together, and trying to make them work together without language
additions related to tail-const is just going to cause you grief in the long
run.

--


More information about the Digitalmars-d-bugs mailing list