Parameter modes there are four parameter modes each

This preview shows page 201 - 209 out of 266 pages.

Parameter Modes There are four parameter modes , each of which indicates a possible way that a method might change the value of the corresponding argument Parameter modes help us in three ways: – They concisely summarize which arguments might have their values modified by a call – They make requires/ensures clauses shorter – They allow us to perform “sanity checks” of contracts against certain simple errors 7 January 2019 OSU CSE 4
Parameter Modes There are four parameter modes , each of which indicates a possible way that a method might change the value of the corresponding argument Parameter modes help us in three ways: – They concisely summarize which arguments might have their values modified by a call – They make requires/ensures clauses shorter – They allow us to perform “sanity checks” of contracts against certain simple errors 7 January 2019 OSU CSE 5 Modes are listed for the formal parameters, including this , but actually apply to their corresponding arguments for a call, including the receiver.
Restores Mode Upon return from a method call, a restores-mode parameter once again has its incoming value – Equivalent to adding, e.g., ... and x = #x to the ensures clause – An old restores-mode parameter, e.g., #x , should not appear in the ensures clause – This is the default parameter mode , so if a parameter is not listed with some other mode then its mode is restores 7 January 2019 OSU CSE 6
Clears Mode Upon return from a method call, a clears- mode parameter has an initial value for its type, i.e., a value that an assignment of the no-argument constructor could give it – Equivalent to adding, e.g., ... and x = [an initial value for its type] to the ensures clause – A clears-mode parameter, e.g., x , should not appear in the ensures clause except as #x 7 January 2019 OSU CSE 7
Clears Mode Upon return from a method call, a clears- mode parameter has an initial value for its type, i.e., a value that an assignment of the no-argument constructor could give it – Equivalent to adding, e.g., ... and x = [an initial value for its type] to the ensures clause – A clears-mode parameter, e.g., x , should not appear in the ensures clause except as #x 7 January 2019 OSU CSE 8 It’s possible there isn’t a no- argument constructor; see the contract for the clear method in interface Standard for technical details.
Example void transferFrom(NaturalNumber n) • Sets this to the incoming value of n , and resets n to an initial value. • Replaces: this • Clears: n • Ensures: this = #n 7 January 2019 OSU CSE 9
Replaces Mode Upon return from a method call, a replaces-mode parameter has a value that might be changed from its incoming value, but the method’s behavior does not depend on its incoming value – A replaces-mode parameter, e.g., x , should not appear in the requires clause, and #x should not appear in the ensures clause 7 January 2019 OSU CSE 10
Example void copyFrom(NaturalNumber n) • Copies n to this .

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture