No bounds checking for dynamic arrays at compile time?

bearophile bearophileHUGS at lycos.com
Thu Dec 13 03:07:55 PST 2012


Walter Bright:

> Well, it should for those cases.

OK, then I'll split that enhancement request into a bug report.


> Yeah, it does, unless you care to put in the compiler a long 
> list of special cases. For example, what about $+$, n+$, 
> $+$-$+1, $<<n, etc.? These sort of turn into whack-a-mole games.

I agree that putting lot of similar special cased tests in the 
compiler is a bad idea (also because code like $+$-$+1 is very 
uncommon).
But can't the already present expression range analysis be used 
to cover some simple but common enough bugs?


> Since the bug is caught anyway, such is an extremely low 
> priority because it's got such a low payoff.

If that's true then why aren't you programming in Python? :-) 
Spotting mistakes at compile time is usually better because the 
sooner you find bugs, the less problems they cause. And because 
compile-time tests work on all code paths, while run-time tests 
only work on the code actively run (so bugs in uncommonly run 
code paths sometimes do not get caught, and maybe are caught much 
later by a client running the code). This is one of the main 
advantages of static typing over unit-testing.

Bye,
bearophile


More information about the Digitalmars-d mailing list