25.07.2014 Views

VDM-10 Language Manual

VDM-10 Language Manual

VDM-10 Language Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12. Operation Definitions<br />

here. However there is a conceptual distinction between them which dictates which should<br />

be used and when. The pre-condition specifies what callers to the operation must ensure<br />

for correct behaviour; the exception clauses indicate that the operation being specified takes<br />

responsibility for error handling when an exception condition is satisfied. Hence normally<br />

exception clauses and pre-conditions do not overlap.<br />

The next <strong>VDM</strong>-SL example of an operation uses the following state definition:<br />

✞<br />

✡✝<br />

state qsys of<br />

q : Queue<br />

end<br />

The next <strong>VDM</strong>++/<strong>VDM</strong>-RT example of an operation uses the following instance variable<br />

definition:<br />

✞<br />

✡✝<br />

instance variables<br />

q : Queue<br />

✆<br />

✆<br />

This example shows how exceptions with an implicit definition can be used:<br />

✞<br />

✡✝<br />

DEQUEUE() e: [Elem]<br />

ext wr q : Queue<br />

post q˜ = [e] ˆ q<br />

errs QUEUE_EMPTY: q = [] -> q = q˜ and e = nil<br />

✆<br />

This is a dequeue operation which uses a global variable q of type Queue to get an element<br />

e of type Elem out of the queue. The exceptional case here is that the queue in which the<br />

exception clause specifies how the operation should behave is empty.<br />

Note that the <strong>VDM</strong>-SL interpreter creates a function here:<br />

✞<br />

✡✝<br />

post_DEQUEUE: [Elem] * qsys * qsys +> bool<br />

✆<br />

12.1 Constructors (<strong>VDM</strong>++ and <strong>VDM</strong>-RT)<br />

Constructors are operations which have the same name as the class in which they are defined and<br />

which create new instances of that class. Their return type must therefore be the same class name,<br />

and if a return value is specified this should be self though this can optionally be omitted.<br />

Multiple constructors can be defined in a single class using operation overloading as described<br />

in section 14.2.<br />

95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!