| 1 | = Contracts = |
| 2 | |
| 3 | Cobra supports contracts which are expressed through lists of assertion expressions |
| 4 | (must-evaluate-true) attached to objects and methods (and properties).[[BR]] |
| 5 | They act as documentation, specification and run-time assertions |
| 6 | and should be viewed as part of the public interface of the items they |
| 7 | are associated with. |
| 8 | |
| 9 | When a contract condition fails, an exception is raised.[[BR]] |
| 10 | The exception message shows the value of each subexpression in the condition, just like the Cobra |
| 11 | 'assert' exception. |
| 12 | For example, 'x > y' failing will show the values of 'x' and 'y'. |
| 13 | |
| 14 | You can turn off contracts when building shipping software in order to boost |
| 15 | run-time performance and/or reduce executable size via the -contracts compiler switch. |
| 16 | |
| 17 | == Keywords == |
| 18 | Class instance invariants are specified as part of the class definition and use the |
| 19 | keyword '''invariant'''. |
| 20 | |
| 21 | Code member preconditions are specified on the code member and use the keyword '''require'''. |
| 22 | |
| 23 | Code member postconditions are also specified on the code member and use the keyword '''ensure'''. |
| 24 | |
| 25 | == Implicit variables related to contracts == |
| 26 | To make writing contracts easier some implicitly declared variables are available for use in expressions |
| 27 | within contracts: |
| 28 | |
| 29 | * '''result''' - available in '''require''' and '''ensure''', |
| 30 | provides a fixed name for the return value of any method or property |
| 31 | |
| 32 | * '''old''' - avail in '''ensure''', |
| 33 | provides an object reference that captures the original state of the object before the method/property executed. |
| 34 | Use to validate a state change. |
| 35 | |
| 36 | In addition there is an additional expression construct |
| 37 | * '''implies''' - available in all contracts. |
| 38 | allows an assertion to be triggered only if a condition is true. |
| 39 | Grammar is |
| 40 | <conditional-expr> implies <assertion-expr> |
| 41 | |
| 42 | e.g. |
| 43 | {{{ |
| 44 | result.count implies result[0].length # non 0-length list item |
| 45 | old.value implies old.value < .value # value non zero and increased |
| 46 | }}} |
| 47 | |
| 48 | == Preconditions == |
| 49 | Preconditions (requirements) are assertion expressions (expressions must evaluate true) |
| 50 | which can be placed on methods and properties.[[BR]] |
| 51 | They check or constrain the state of the current object and/or the state and values of any arguments. |
| 52 | |
| 53 | Keyword is '''require''' |
| 54 | |
| 55 | e.g. |
| 56 | {{{ |
| 57 | def sqroot(i as int) as float |
| 58 | require |
| 59 | i > 0 |
| 60 | body |
| 61 | ... |
| 62 | |
| 63 | def fraction( numer as int, denom as int) as float |
| 64 | require |
| 65 | numer > 0 |
| 66 | denom <> 0 |
| 67 | body |
| 68 | ... |
| 69 | }}} |
| 70 | |
| 71 | == Postconditions == |
| 72 | Post conditions (guarantees) are also assertion expressions (must evaluate true) |
| 73 | and can also be placed on methods and properties.[[BR]] |
| 74 | They check/assert/assure the state of the current object or intended state change |
| 75 | and/or the state and value of any result. |
| 76 | |
| 77 | Keyword is '''ensure''' |
| 78 | |
| 79 | e.g. |
| 80 | {{{ |
| 81 | def sqroot(i as int) as float |
| 82 | ensure |
| 83 | result > 0 |
| 84 | body |
| 85 | ... |
| 86 | |
| 87 | def bumpState( incr as int) as int |
| 88 | require |
| 89 | incr > 0 |
| 90 | ensure |
| 91 | result >= incr |
| 92 | .state = old.state + incr |
| 93 | body |
| 94 | .state += incr |
| 95 | return .state |
| 96 | }}} |
| 97 | |
| 98 | == Object State == |
| 99 | |
| 100 | The 'invariant' keyword leads a clause within a class definition and is |
| 101 | followed by a list of assertion expressions (must evaluate true).[[BR]] |
| 102 | These are executed at the end of each code member (initialiser, method, property, etc.). |
| 103 | They speak to the state of the object. |
| 104 | |
| 105 | Keyword is '''invariant''' |
| 106 | |
| 107 | |
| 108 | == Other == |
| 109 | |
| 110 | Assertions can be sprinkled through the bodies of code members to |
| 111 | verify intermediate state or values of local variables. |
| 112 | |
| 113 | e.g |
| 114 | {{{ |
| 115 | ... |
| 116 | .new = .old + incr |
| 117 | assert .new > .old |
| 118 | }}} |
| 119 | |
| 120 | == Inheritance == |
| 121 | When code member contracts are inherited (and overridden), |
| 122 | the preconditions/requirements ('''require''') can be augmented through an |
| 123 | '''or require''' |
| 124 | clause meaning that the derivedClass is more flexible about what conditions |
| 125 | the work can be done in. |
| 126 | |
| 127 | Similarly postconditions ('''ensure''') can be strengthened with an |
| 128 | '''and ensure''' |
| 129 | clause meaning that the derived class can guarantee additional conditions |
| 130 | about the results of the work. |
| 131 | |
| 132 | |
| 133 | == Compiler Command Line == |
| 134 | The -contracts commandline switch controls contracts code generation: |
| 135 | values are |
| 136 | * none - suppress contracts code generation entirely |
| 137 | * inline (default) - place contracts code inline to the item the contract is associated with |
| 138 | * method - place contracts code in a separate method and provide a method call from the associated item |
| 139 | |
| 140 | Turn off contracts with |
| 141 | cobra -contracts:none ... |
| 142 | |
| 143 | == Grammar == |
| 144 | {{{ |
| 145 | require |
| 146 | BODY |
| 147 | |
| 148 | ensure |
| 149 | BODY |
| 150 | |
| 151 | invariant |
| 152 | BODY |
| 153 | }}} |
| 154 | |
| 155 | |
| 156 | If a contract has only one condition, you can write it |
| 157 | on one line and skip 'body'. |
| 158 | {{{ |
| 159 | require EXPR |
| 160 | ensure EXPR |
| 161 | invariant EXPR |
| 162 | }}} |
| 163 | |
| 164 | |
| 165 | == Examples == |
| 166 | |
| 167 | see !HowTo file [http://cobra-language.com/how-to/DeclareContracts/ How To Declare Contracts] |
| 168 | |
| 169 | |