[Issue 13185] Detect precondition violations at compile time when possible
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Jul 22 06:34:31 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=13185
bearophile_hugs at eml.cc changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bearophile_hugs at eml.cc
--- Comment #1 from bearophile_hugs at eml.cc ---
(In reply to yebblies from comment #0)
> In many cases, the compiler can easily detect that a function call cannot
> possibly pass the precondition.
>
> int divide(int x, int y)
> in
> {
> assert(y != 0);
> }
> body
> {
> return x / y;
> }
>
> void main()
> {
> func(3);
> divide(9, 0);
> }
>
> In these cases we can produce an error at compile time.
In my opinion it's much better to offer the programmer an explicit way to tell
the compiler that a compile-time testing (where/if possible) is desired, using
an "enum precondition":
int divide(in int x, in int y)
enum in { // Enum pre-condition.
assert(y != 0);
in { // Regular pre-condition.
assert(y != 0);
} body {
return x / y;
}
void main() {
divide(9, 0);
}
--
More information about the Digitalmars-d-bugs
mailing list