Skip to content

Commit

Permalink
Adding subsection on constrained array types
Browse files Browse the repository at this point in the history
  • Loading branch information
gusthoff committed Oct 20, 2024
1 parent 97d1b5e commit c0821b1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions content/courses/advanced-ada/parts/data_types/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ its whole lifetime:
-- Bounds cannot be changed!


.. _Adv_Ada_Constrained_Array_Type:

Constrained array types
~~~~~~~~~~~~~~~~~~~~~~~

Note that we could declare constrained array types. Let's rework the previous
example:

.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Arrays.Array_Constraints.Constrained_Array_Type

package Measurement_Defs is

type Measurements is
array (1 .. 10) of Float;
-- ^ Bounds are of known and fixed.

end Measurement_Defs;

with Ada.Text_IO; use Ada.Text_IO;

with Measurement_Defs; use Measurement_Defs;

procedure Show_Measurements is
M : Measurements;
-- ^ We cannot change the
-- bounds here!
begin
Put_Line ("First index: " & M'First'Image);
Put_Line ("Last index: " & M'Last'Image);
end Show_Measurements;

In this case, the bounds of the :ada:`Measurements` type are fixed. Now, we
cannot specify the bounds (or change them) in the declaration of the :ada:`M`
array, as they have already been defined in the type declaration.


Unconstrained Arrays vs. Vectors
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down

0 comments on commit c0821b1

Please sign in to comment.