foreach() behavior on ranges
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Wed Aug 25 17:25:07 UTC 2021
On Wednesday, 25 August 2021 at 17:01:54 UTC, Steven
Schveighoffer wrote:
> In a world where copyability means it's a forward range? Yes.
> We aren't in that world, it's a hypothetical "if we could go
> back and redesign".
OK, that makes sense.
> Technically this is true. In practice, it rarely happens. The
> flaw of `save` isn't that it's an unsound API, the flaw is that
> people get away with just copying, and it works 99.9% of the
> time. So code is simply untested with ranges where `save` is
> important.
This is very true, and makes it quite reasonable to try to pursue
"the obvious/lazy thing == the thing you're supposed to do"
w.r.t. how ranges are defined.
> I'd be willing to bet $10 there is a function in phobos right
> now, that takes forward ranges, and forgets to call `save` when
> iterating with foreach. It's just so easy to do, and works with
> most ranges in existence.
I'm sure you'd win that bet!
> The idea is to make the meaning of a range copy not ambiguous.
Yes, this feels reasonable. And then one can reserve the idea of
a magic deep-copy method for special cases like pseudo-RNGs where
one wants them to be copyable on user request, but without code
assuming it can copy them.
More information about the Digitalmars-d-learn
mailing list