Invariants (keyword)
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
Invariants form part of Delphi Prism's Class Contracts feature.
In contrast to pre and post conditions, invariants are used to define a fixed state the object must fulfill at any given time. Invariants can be public or private, depending on how often their content is verified. Public invariants will be checked at the end of every public method (after the method's "ensure" block, if present, has been checked), and if an invariant fails, an exception is raised.
Private invariants will be checked at the end of every method call, public or private. Example:
type MyClass = class public //some methods and/or properties public invariants fField1 > 35; SomeProperty = 0; SomeBoolMethod() and not (fField2 = 5); private invariants fField > 0; end;
Note:
- both types of invariant sections have full access to all private fields of the class, the only difference is the methods (and property access) to which they apply.
- if a class specifies invariants, all fields used in an invariant must be marked as private.
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To