Changes between Version 8 and Version 9 of Methods
- Timestamp:
- 12/03/13 09:24:01 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Methods
v8 v9 66 66 sum takes a variable number of integer args and returns an integer. 67 67 68 == Tests, Contracts and Code == 69 70 The above refers to simple method code or method body code. [[BR]] 71 You can also specify Tests (for the method) and contract requirements on the method. [[BR]] 72 To do this the method is tagged with named clauses: [[BR]] 73 * '''test''' clauses for any tests, 74 * '''require''' and '''ensure''' clauses for the pre and post conditions specifying the method contracts and a 75 * '''body''' clause for the method code. [[BR]] 76 77 If given, the order of these clauses must be : 78 * Test clauses 79 * contract clauses (require and/or ensure) 80 * body clause 81 68 82 == Special Methods == 69 83 These are named methods on a class that have some special responsibility or relationship to the Object Model[[BR]] … … 157 171 .counter += 1 #method body - not additionally indented 158 172 .val += i 159 return .val 173 return .val 174 175 # Heres a full example giving all the clauses 176 def simpleBump(i as int) as int 177 require # prerequisite contract - in indented clause (1 or many expressions) 178 i >0 179 ensure # postcondition contract - in indented clause (1 or many expressions) 180 .val > old .val 181 test # unit test clause 182 a = Inst() 183 assert a.counter == 0 184 assert a.val == 0 185 a.simpleBump(1) 186 assert a.counter ==1 187 assert a.val == 1 188 test num2 # another unit test clause (named) 189 a = Inst() 190 a.simpleBump(2) 191 assert a.counter == 1 192 assert a.val == 2 193 194 body #method body - tagged and additionally indented 195 .counter += 1 196 .val += i 197 return .val 198 160 199 }}} 200 201 Return to LanguageTopics