Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Assertions/D/assertions.d
Normal file
36
Task/Assertions/D/assertions.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import std.exception: enforce;
|
||||
|
||||
int foo(in bool condition) pure nothrow
|
||||
in {
|
||||
// Assertions are used in contract programming.
|
||||
assert(condition);
|
||||
} out(result) {
|
||||
assert(result > 0);
|
||||
} body {
|
||||
if (condition)
|
||||
return 42;
|
||||
|
||||
// assert(false) is never stripped from the code, it generates an
|
||||
// error in debug builds, and it becomes a HALT instruction in
|
||||
// -release mode.
|
||||
//
|
||||
// It's used as a mark by the D type system. If you remove this
|
||||
// line the compiles gives an error:
|
||||
//
|
||||
// Error: function assertions.foo no return exp;
|
||||
// or assert(0); at end of function
|
||||
assert(false, "This can't happen.");
|
||||
}
|
||||
|
||||
void main() pure {
|
||||
int x = foo(true);
|
||||
|
||||
// A regular assertion, it throws an error.
|
||||
// Use -release to disable it.
|
||||
// It can be used in nothrow functions.
|
||||
assert(x == 42, "x is not 42");
|
||||
|
||||
// This throws an exception and it can't be disabled.
|
||||
// There are some different versions of this lazy function.
|
||||
enforce(x == 42, "x is not 42");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue