Out of bound problem

Sergey Gromov snake.scaly at gmail.com
Sat Feb 16 19:22:39 PST 2008


Christopher Wright <dhasenan at gmail.com> wrote:
> if ("".length == 0) {
>     // blah
> } else if ("".length == 1) {
>     result = foo(blah, ""[0]);
> }
> 
> Then the compiler sees: Array literal! Constant index! Must expand!
> Obviously, it can't do that. So it gives an error.
> 
> [...]
> But yeah, it's a bug.

But how the compiler should handle this ?  In trivial case above, the 
code with error is unreachable.  But as a generic solution, it probably 
should expand into something like:

if (condition) {
    // blah
} else if (other condition) {
    blah;  // stays here for possible side effects
    // ""[0] is replaced with this at compile time
    throw new ArrayBoundsException();
    // the rest of the block is unreachable, throw away
}

-- 
SnakE


More information about the Digitalmars-d-learn mailing list