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
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
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
;
>
> 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) {} /
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
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
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
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
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;