LEVIATHAN vd52e79c4 · d52e79c486

Standard Library

interface IContractReporter

Receives every contract violation before it is thrown.

since 0.1.0-alpha.1linuxwindowswasm

Overview

A program installs one with a top-level bind IContractReporter => MyReporter(); in any of its files: that binding reaches every call site of the program, its dependencies and the standard library. A bind inside a function body covers only the calls written in that body. With no binding, nothing is called and the violation's message is the report. The reporter cannot stop the violation: when report returns the violation continues, and an exception it throws is attached to the violation. To end the process, call env::exit from report.

Examples

use io::console;
use std::IContractReporter;

class Printer : IContractReporter {
    void report(ContractViolation v) { console.writeln("contract: " + v.predicate); }
}

IContractReporter r = Printer();
r.report(ContractViolation(ContractKind::Assertion, "ok", "", ""));
contract: ok

Methods

report

report(ContractViolation v) -> void

Sees one violation, once, before it propagates.

Parameters

v
The violation; its fields hold the report.

Examples

use io::console;
use std::IContractReporter;

class Counter : IContractReporter {
    int seen = 0;
    void report(ContractViolation v) { seen = seen + 1; }
}

Counter c = Counter();
c.report(ContractViolation(ContractKind::Precondition, "n > 0", "f", ""));
console.writeln(c.seen);
1

See also

  • ContractViolation — A violated contract: a failed pre, post, assert or invariant.