On Sunday, 22 December 2013 at 07:29:22 UTC, ilya-stromberg wrote:
On Saturday, 21 December 2013 at 20:43:27 UTC, bearophile wrote:
3) Just like the integer '5' a range of values as 0 .. 1000 is
an immutable value. So a variable that scans such range should
be immutable. If you really want to mutate such variable you
should add a modifier like "mutable" or "mut" or something.
Another common trap in D coding is iterating on an array of
structs with foreach, mutating the current struct and
forgetting that you are mutating only a _copy_ of the items.
Unfortunately there is no mutable keyword in D, and Walter
rejected all this idea. So the next best thing it to always
put "immutable" at the foreach variable, unless you want to
mutate it or if you can't use const/immutable for some other
Why did Walter reject this idea?
BTW, we don't need `mutable` keyword to implement this idea. We
should just deny any mutation of item copy. If you really need
to store temporary result, add new variable. For example:
++i; //error - this variable contains copy of data, not a
ref to the original data
auto temp_i = i + 1; //OK
We already have similar errors, for example:
i; //Error: var has no effect in expression (i)
Those are quite different. The first one does have an effect,
it's just that the effect is only local to the loop scope. Even
that isn't guaranteed, as ++i could have side-effects.