Skip to content

Commit 2510ee7

Browse files
committed
subtypes: add example use of 'Valid
1 parent 1bfa458 commit 2510ee7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

courses/fundamentals_of_ada/030_basic_types/11-subtypes_full_picture.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,37 @@ Attributes Reflect the Underlying Type
181181
Shade : Color range Red .. Blue := Brown; -- run-time error
182182
Hue : Rainbow := Rainbow'Succ (Blue); -- run-time error
183183
184+
---------------
185+
Valid attribute
186+
---------------
187+
188+
* :ada:`T'Valid` is a :ada:`Boolean`
189+
* :ada:`True` |rightarrow| the current representation for the given scalar is valid
190+
191+
.. code:: Ada
192+
193+
procedure Main is
194+
type Small_Odd is new Integer range 0 .. 5
195+
with Predicate => Small_Odd mod 2 = 1;
196+
O : aliased Small_Odd;
197+
O_Alias : aliased Integer with Address => O'Address;
198+
begin
199+
for I in 0 .. 10 loop
200+
O_Alias := I;
201+
Ada.Text_IO.Put_Line (Boolean'Image (O'Valid));
202+
end loop;
203+
end Main;
204+
205+
.. code::
206+
207+
FALSE
208+
TRUE
209+
FALSE
210+
TRUE
211+
FALSE
212+
TRUE
213+
FALSE
214+
184215
------------------------
185216
Idiom: Extended Ranges
186217
------------------------

0 commit comments

Comments
 (0)