...
/out Blocks and Expression-based Contracts
out Blocks and Expression-based Contracts
This lesson explains the use of out blocks for postconditions and the expression-based contract. Furthermore, it teaches how to disable contract programming in D.
We'll cover the following...
out blocks for postconditions #
This contract involves guarantees that the function provides. Such guarantees are called the function’s postconditions. An example of a function with a postcondition would be a function that returns the number of days in February: The function can guarantee that the returned value would always be either 28 or 29.
The postconditions are checked inside the
out
blocks of functions.
Because the value that a function returns by need not be defined as a variable inside the function, there is usually no name to refer to the return value. This can be seen as a problem because the assert
checks inside the out
block cannot refer to the returned variable by name.
D solves this problem by providing a way of naming the return value right after the out
keyword. That ...