legacy code retreat's triva game : the D version

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Sat Dec 21 23:29:20 PST 2013


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 reason.

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:

foreach(i; arr)
{
    ++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:

void foo()
{
    int i;
    i; //Error: var has no effect in expression (i)
}


More information about the Digitalmars-d-announce mailing list