Ada Programming/Attributes/'Valid
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Description
The Valid attribute can be used with an object of any scalar type (that is, numeric or enumeration types) to know whether its value is valid (e.g. not out-of-range, etc). The result is always True or False; neither CONSTRAINT_ERROR nor any other exception are ever raised.
[edit] Example
-- Declare a discrete type and explicitly set the position numbers type My_Enum is (Value1, Value2, Value3); for My_Enum use (Value1 => 2, Value2 => 4, Value3 => 6 ); Result : Natural; Enum_Var : My_Enum; Sneaky_Back_Door : Integer; for Enum_Var'Address use Sneaky_Back_Door'Address; ... if not Result'Valid then -- Result is out-of-range, it has a negative value Result := Natural'First; end if; ... -- Assign a bad integer value to the enumerated type variable. Sneaky_Back_Door := 1; ... if not Enum_Var'Valid then -- Enum_Var contains a bad value Enum_Var := My_Enum'First; end if;
[edit] See also
[edit] Wikibook
[edit] Ada Reference Manual
[edit] Ada Quality and Style Guide
- 5.9.1 Unchecked Conversion — Consider using the 'Valid attribute to check the validity of scalar data.
- 5.9.7 Direct_IO and Sequential_IO — Use the 'Valid attribute to check the validity of scalar values obtained through Ada.Direct_IO and Ada.Sequential_IO.
- 6.3.3 The Abort Statement — In the presence of the abort statement, consider using the 'Valid attribute to check the validity of scalar data.