Skip to content

Commit 6fa888e

Browse files
committed
Adding section on discriminant constraints
1 parent 2290833 commit 6fa888e

File tree

1 file changed

+93
-0
lines changed
  • content/courses/advanced-ada/parts/data_types

1 file changed

+93
-0
lines changed

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,11 +1071,15 @@ component :ada:`Arr`. Note that the same discriminant part must appear in both
10711071
:ref:`the partial and the full view <Adv_Ada_Type_View>` of type :ada:`T`.
10721072

10731073

1074+
.. _Adv_Ada_Record_Discriminants_Object_Declaration:
1075+
10741076
Object declaration
10751077
~~~~~~~~~~~~~~~~~~
10761078

10771079
As we've already seen, we declare objects of a type :ada:`T` with a
10781080
discriminant :ada:`D` by specifying the actual value of discriminant :ada:`D`.
1081+
This is called a
1082+
:ref:`discriminant constraint <Adv_Ada_Record_Discriminant_Constraints>`.
10791083
For example:
10801084

10811085
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants.Objects_Discriminants
@@ -2122,6 +2126,94 @@ operations related to discriminants |mdash| more specifically, the
21222126
- :arm:`3.7.1 Discriminant Constraints <3-7-1>`
21232127

21242128

2129+
.. _Adv_Ada_Record_Discriminant_Constraints:
2130+
2131+
Discriminant constraints
2132+
~~~~~~~~~~~~~~~~~~~~~~~~
2133+
2134+
As we discussed before, when
2135+
:ref:`declaring an object with a discriminant <Adv_Ada_Record_Discriminants_Object_Declaration>`,
2136+
we have to specify the values of the all discriminants |mdash| unless, of
2137+
course, those discriminants have a
2138+
:ref:`default value <Adv_Ada_Record_Discriminants_Default_Values>`. The values
2139+
we specify for the discriminants are called discriminant constraints.
2140+
2141+
Let's revisit the code example we've seen earlier on:
2142+
2143+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants_Constraints_Operations.Discriminant_Constraint
2144+
2145+
package Recs is
2146+
2147+
type T (L : Positive;
2148+
M : Positive) is
2149+
null record;
2150+
2151+
end Recs;
2152+
2153+
with Recs; use Recs;
2154+
2155+
procedure Show_Object_Declaration is
2156+
A : T (L => 5, M => 6);
2157+
B : T (7, 8);
2158+
C : T (7, M => 8);
2159+
begin
2160+
null;
2161+
end Show_Object_Declaration;
2162+
2163+
Here, :ada:`L => 5, M => 6` (for object :ada:`A`) are named constraints, while
2164+
:ada:`7, 8` (for object :ada:`B`) are positional constraints.
2165+
2166+
It's possible to use both positional and named constraints, as we do for object
2167+
:ada:`C`: :ada:`7, M => 8`. In this case, the positional associations must
2168+
precede the named associations.
2169+
2170+
In the case of named constraints, we can use multiple selector names:
2171+
2172+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants_Constraints_Operations.Discriminant_Constraint
2173+
2174+
with Recs; use Recs;
2175+
2176+
procedure Show_Object_Declaration is
2177+
A : T (L | M => 5);
2178+
-- ^^^^^
2179+
-- multiple selector names
2180+
begin
2181+
null;
2182+
end Show_Object_Declaration;
2183+
2184+
This is only possible, however, if those named discriminants are all of the
2185+
same type. (In this case, :ada:`L` and :ada:`M` are both of :ada:`Positive`
2186+
subtype.)
2187+
2188+
.. admonition:: In the Ada Reference Manual
2189+
2190+
- :arm22:`3.7.1 Discriminant Constraints <3-7-1>`
2191+
2192+
2193+
Discriminant constraint in subtypes
2194+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2195+
2196+
We can use discriminant constraints in the declaration of subtypes. For
2197+
example:
2198+
2199+
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Records.Discriminants_Constraints_Operations.Discriminant_Constraint
2200+
2201+
with Recs; use Recs;
2202+
2203+
procedure Show_Object_Declaration is
2204+
subtype T_5_6 is T (L => 5, M => 6);
2205+
-- ^^^^^^^^^^^^^^
2206+
-- discriminant constraints for subtype
2207+
2208+
A : T_5_6;
2209+
begin
2210+
null;
2211+
end Show_Object_Declaration;
2212+
2213+
In this example, we use the named discriminant constraints
2214+
:ada:`L => 5, M => 6` in the declaration of the subtype :ada:`T_5_6`.
2215+
2216+
21252217
.. _Adv_Ada_Constrained_Attribute:
21262218

21272219
Constrained Attribute
@@ -2322,6 +2414,7 @@ the value of the :ada:`Extended` discriminant). Therefore, with the call to
23222414
- :arm22:`3.7.2 Operations of Discriminated Types <3-7-2>`
23232415

23242416

2417+
23252418
..
23262419
TO BE DONE:
23272420

0 commit comments

Comments
 (0)