Ada Programming/Attributes/'Valid

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Ada Lovelace 1838.jpg

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

[edit] Ada Rationale