How to foreach over a DList?

anonymous anonymous at example.com
Mon Mar 31 15:42:33 PDT 2014


On Monday, 31 March 2014 at 21:55:03 UTC, monarch_dodra wrote:
> If opSlice was const, then you'd get a const slice, with const 
> reference. You wouldn't even be able to iterate on it.

const opSlice does not mean it returns a const(Range).

> With some extra code, you could create some sort of "slice of 
> immutable" type, but you'd still only be able to get const 
> items.

Iterating over const items is the goal.

> We *could* add a const *overload* but...
>
>> Or avoid using const for containers if you can.
>
> That. D's "turtles all the way down" const doesn't work like 
> C++'s "head only" const. As a general rule, don't use too much 
> const in D, *especially* for containers-like objects...
>
> At this point, adding a const overload would be nothing more 
> that a still unusable mess, with extra maintenance overhead.

Gotta disagree violently here. Asking for a const overload of
opSlice isn't "too much const". std.container is just in a sorry
state regarding const compatibility. That can and should be fixed.


More information about the Digitalmars-d-learn mailing list