Assertions

Sather - A Language Tutorial
Prev | Chapter 11. Safety Features | Next

# 11.3. Assertions

Assertions are not part of the interface to a routine. Rather, they are an internal consistency check within a piece of code, to ensure that the computation is proceeding as expected.

## 11.3.1. assert statements

assert statements specify a boolean expression that must evaluate to `true`; otherwise it is a fatal error.

    private attr arr:ARRAY{INT};
    ...
    sum_of_elts is
       sum:INT := 0;
       loop e ::= arr.elt!;
          assert e > 0;
          sum := sum + e;
       end;
       return sum;
    end;

In the above piece of code, we expect the class to only be storing postive values in the array 'arr' . To double check this, when adding the elements together, we check whether each element is positive.

Prev | Home | Next
Postconditions | Up | Invariants