spir <denis.s...@gmail.com> wrote:
On Sun, 21 Nov 2010 20:21:14 -0500
bearophile <bearophileh...@lycos.com> 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
// 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, an
inevitable one? Is this an enhancement request worth adding to Bugzilla?
Bye and thank you,
bearophile
Maybe you'll find it weird, but I would expect
foreach (const(auto) x; array) {};
to be the logical idiom for this. "auto" beeing a kind of placeholder
for a type name.
'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;
immutable y = 4;
--
Simen