Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.
Version 2 (modified by csauls, 18 years ago)
Updated to use writef.

Assertions

Part of TutorialFundamentals

Description

If you can determine it's wrong at compile-time, why bother running it?

If an assert evaluates to "false", the program terminates and an error message is displayed.

Example

import std.stdio;

void main() {
    writefln("Let's test some assertions...");

    /* These assertions should succeed -- they evaluate to true. */

    assert(true);
    assert('\xAA' == '\u00AA');
    assert(221 % 5 == 1);
    assert(25 * 4 * 20 + 4 == 2004);

    /*  This assertion fails... 
        Error: AssertError Failure assert.d(line number) */

    assert(false);    
    writefln("No problem, man.");
}

More Information

See also the D Specification.

Source

Posted by csauls
Date/Time Mon Jan 2, 2006 4:36 am