Ada Programming/Attributes/'Valid

Description
Objects may become invalid via import, unchecked conversions, overlays, etc.

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.

It is important that the evaluation of the attribute does not count as reading the object, whereas reading an invalid object makes the program erroneous.

Example
My_Enum (Value1, Value2, Value3); My_Enum (Value1 => 2, Value2 => 4, Value3 => 6 ); Result            : Natural; Enum_Var, Other_Var: My_Enum; Sneaky_Back_Door  : Integer; Enum_Var'Address Sneaky_Back_Door'Address; ...  Result' Result := Natural'; ; ... Sneaky_Back_Door := 1; Other_Var := Enum_Var; ...  Enum_Var' Enum_Var := My_Enum'; ;

Note that in the erroneous assignment statement above, no range check is performed (hence no exception will be raised) since both variables are of the same subtype.

Wikibook

 * Ada Programming
 * Ada Programming/Attributes

Ada Quality and Style Guide

 * &mdash; Consider using the 'Valid attribute to check the validity of scalar data.
 * &mdash; Use the 'Valid attribute to check the validity of scalar values obtained through Ada.Direct_IO and Ada.Sequential_IO.
 * &mdash; In the presence of the abort statement, consider using the 'Valid attribute to check the validity of scalar data.

Ada Rationale


|Valid