Const foreach

spir denis.spir at gmail.com
Mon Nov 22 04:40:33 PST 2010


On Sun, 21 Nov 2010 20:21:14 -0500
bearophile <bearophileHUGS at 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.
Presently yields the error:
    __trials__.d(25): basic type expected, not auto

denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list