Re: Const foreach

2010-11-22 Thread Simen kjaeraas
Pelle Månsson 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. Try it: const x = 4; immu

Re: Const foreach

2010-11-22 Thread Pelle Månsson
ut 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 } Is something wrong in that code? Is this a

Re: Const foreach

2010-11-22 Thread Simen kjaeraas
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 wrong in that cod

Re: Const foreach

2010-11-22 Thread spir
; > > 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) {} /

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

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 Simen kjaeraas
bearophile wrote: 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 // fo

Re: Const foreach

2010-11-21 Thread Jonathan M Davis
id 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

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;