Const foreach

bearophile bearophileHUGS at lycos.com
Sun Nov 21 18:37:01 PST 2010


Jonathan M Davis:

> Actually, const is pointless in your example, since you're dealing with a value 
> type.

A const value time is meaningful, it means that you are saying the D compiler that you don't want to modify it. Generally it's good to stick a const/immutable even when you use values inside your functions.


> And unfortunately, I don't 
> think that it works to use const ref in a foreach (I've never gotten it work 
> anyway)

I think it works (it has the same problems of the const alone, there is no type inference):

void main() {
    int[3] array;
    foreach (ref const(int) x; array) {}
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list