Re: Const foreach

2010-11-22 Thread spir
main() { int[3] array; // not const // foreach (const x; array) {}// Error // foreach (const auto x; array) {} // Error // foreach (const(int) x; array) {} // OK foreach (const(typeof(array[0])) x; array) {} // OK } Is something

Re: Const foreach

2010-11-22 Thread Pelle Månsson
variable to be const. This is possible, but it seems I lose type inference: void main() { int[3] array; // not const // foreach (const x; array) {} // Error // foreach (const auto x; array) {} // Error // foreach (const(int) x; array) {} // OK foreach (const(typeof(array[0])) x; array) {} // OK

Re: Const foreach

2010-11-22 Thread Simen kjaeraas
Pelle Månsson pelle.mans...@gmail.com wrote: 'auto' is not a placeholder for a type, but the default storage class. IOW, 'int n;' == 'auto int n;'. This does however not compile, complaining that it has no effect. Specifying just the storage class signals the compiler to use type inference.

Const foreach

2010-11-21 Thread bearophile
If in a D2 program I have an array of mutable items I may want to iterate on them but not modify them, so I'd like the iteration variable to be const. This is possible, but it seems I lose type inference: void main() { int[3] array; // not const // foreach (const x; array

Re: Const foreach

2010-11-21 Thread Simen kjaeraas
; // not const // foreach (const x; array) {}// Error // foreach (const auto x; array) {} // Error // foreach (const(int) x; array) {} // OK foreach (const(typeof(array[0])) x; array) {} // OK } Is something wrong in that code? Is this a known limitation

Re: Const foreach

2010-11-21 Thread bearophile
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

Re: Const foreach

2010-11-21 Thread Jonathan M Davis
On Sunday 21 November 2010 18:37:01 bearophile wrote: 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