Types as pre/post conditions

Types can be thought of as static (compile-time) preconditions and postconditions rather than dynamic (run-time) preconditions and postconditions. For example:

int addOne(int x)
{
  return (x+1);
}
      

The int type of x is a static precondition guaranteeing that addOne will only be called with values that are ints. The int return type is a static postcondition guaranteeing that addOne always returns a value that is an int.

Types - (static preconditions and postconditions in C++) can be checked by the compiler. Dynamic preconditions and postconditions depend on run-time values. They cannot be checked in advance. Thus, it's up to the implementer to check explicit that they are met. For example:

int Bag::grab()
// precondition: bag is not empty
// postcondition: an arbitrary int from bag 
// is returned and 
// bag has 1 less element
{
  // assume the existence of a member function
  // bool Bag::isEmpty()

  // check that the precondition is met
  assert (!isEmpty()); 
.
.
.