On 11/22/2010 04:12 PM, Simen kjaeraas wrote:
spir <[email protected]> wrote:

On Sun, 21 Nov 2010 20:21:14 -0500
bearophile <[email protected]> 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;


I'm not sure you are correct:

    const static auto x = 4;

Reply via email to