Skip to content

Commit 2290833

Browse files
committed
Adding simple example for Constrained attribute
1 parent c1f8754 commit 2290833

File tree

1 file changed

+40
-4
lines changed
  • content/courses/advanced-ada/parts/data_types

1 file changed

+40
-4
lines changed

content/courses/advanced-ada/parts/data_types/records.rst

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,10 +2128,46 @@ Constrained Attribute
21282128
~~~~~~~~~~~~~~~~~~~~~
21292129

21302130
We can use the :ada:`Constrained` attribute to verify whether an object of
2131-
discriminated type is constrained or not. Let's start our discussion by reusing
2132-
the :ada:`Simple_Record` type from previous examples. In this version of the
2133-
:ada:`Unconstrained_Types` package, we're adding a :ada:`Reset` procedure for
2134-
the discriminated record type:
2131+
discriminated type is constrained or not. Let's look at a simple example:
2132+
2133+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants_Constraints_Operations.Simple_Constrained_Attribute
2134+
2135+
package Recs is
2136+
2137+
type T (L : Positive := 1) is
2138+
null record;
2139+
2140+
end Recs;
2141+
2142+
with Ada.Text_IO; use Ada.Text_IO;
2143+
2144+
with Recs; use Recs;
2145+
2146+
procedure Show_Constrained_Attribute is
2147+
Constr : T (L => 5);
2148+
-- ^^^^^^ constrained.
2149+
Unconstr : T;
2150+
-- ^ unconstrained;
2151+
-- using defaults.
2152+
begin
2153+
Put_Line ("Constr'Constrained: "
2154+
& Constr'Constrained'Image);
2155+
Put_Line ("Unconstr'Constrained: "
2156+
& Unconstr'Constrained'Image);
2157+
end Show_Constrained_Attribute;
2158+
2159+
As the :ada:`Constrained` attribute indicates, the :ada:`Constr` object is
2160+
constrained (by the :ada:`L => 5` discriminant constraint), while the
2161+
:ada:`Unconstr` object is unconstrained. Note that, even though :ada:`Unconstr`
2162+
is using the default value for :ada:`L` |mdash| which would correspond to the
2163+
discriminant constraint :ada:`L => 1` |mdash| the object itself hasn't been
2164+
constraint at its declaration.
2165+
2166+
Let's continue our discussion with a more complex example by reusing
2167+
the :ada:`Unconstrained_Types` package that we declared in a
2168+
:ref:`previous section <Adv_Ada_Definite_Indefinite_Subtypes>`. In this
2169+
version of the package, we're adding a :ada:`Reset` procedure for the
2170+
discriminated record type :ada:`Simple_Record`:
21352171

21362172
.. code:: ada compile_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants_Constraints_Operations.Constrained_Attribute
21372173

0 commit comments

Comments
 (0)