From 358b34ee1ffa4332c3943bdaf0969abf1f64a8e1 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Tue, 4 Mar 2025 20:16:13 -0300 Subject: [PATCH 01/13] Editorial change: adding todo item --- .../advanced-ada/parts/data_types/arrays.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/content/courses/advanced-ada/parts/data_types/arrays.rst b/content/courses/advanced-ada/parts/data_types/arrays.rst index 039f16840..3d7b405dc 100644 --- a/content/courses/advanced-ada/parts/data_types/arrays.rst +++ b/content/courses/advanced-ada/parts/data_types/arrays.rst @@ -766,3 +766,17 @@ that the length be 20 at most, and it's 40 in this case. Also, the assignment to :ada:`M_F2` fails because the predicate requires that the first element must be set to :ada:`2.0`, and :ada:`MD10 (1)` has the value 4.0. + +.. + TO BE DONE: + + Slices + ----- + + .. todo:: + + - Simple definition of array slices in Ada + + .. admonition:: In the Ada Reference Manual + + - :arm22:`4.1.2 Slices <4-1-2>` From ea6ee0505206e82f52b60713acf10a81a1a38c83 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Tue, 4 Mar 2025 20:17:39 -0300 Subject: [PATCH 02/13] Editorial change: removing todo item --- .../courses/advanced-ada/parts/data_types/types.rst | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 7d35f9ec3..e7340a54b 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -231,17 +231,12 @@ In this example, we see instances of the following kinds of names: - :arm22:`5.2.1 Target Name Symbols <5-2-1>` -.. - TO BE DONE: +Objects +------- - Objects - ------- - - .. todo:: - - - Definition of an object in Ada - - Difference to object-oriented programming +.. admonition:: In the Ada Reference Manual + - :arm22:`3.3 Objects and Named Numbers <3-3>` Scalar Types ------------ From a88eef3775fdb444bb1962fff0aa1af83cff89a2 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Tue, 4 Mar 2025 20:19:14 -0300 Subject: [PATCH 03/13] Editorial change: adding anchor --- content/courses/advanced-ada/parts/data_types/types.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index e7340a54b..3a5d7f8bb 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -231,6 +231,8 @@ In this example, we see instances of the following kinds of names: - :arm22:`5.2.1 Target Name Symbols <5-2-1>` +.. _Adv_Ada_Objects: + Objects ------- From f91097e4ca5bfd69889d67ffc0ce88742ed9a0d7 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Tue, 4 Mar 2025 20:20:49 -0300 Subject: [PATCH 04/13] Editorial change: adding anchor --- content/courses/intro-to-ada/chapters/exceptions.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/courses/intro-to-ada/chapters/exceptions.rst b/content/courses/intro-to-ada/chapters/exceptions.rst index 18da9c00f..00a043793 100644 --- a/content/courses/intro-to-ada/chapters/exceptions.rst +++ b/content/courses/intro-to-ada/chapters/exceptions.rst @@ -64,6 +64,8 @@ Here, the :ada:`My_Except` exception is raised. We can also specify a message: In this case, we see an additional message when the exception is displayed. +.. _Intro_Ada_Handling_An_Exception: + Handling an exception --------------------- From ffcd3b1c134db8425d661870643a6e434138c769 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Tue, 4 Mar 2025 20:21:18 -0300 Subject: [PATCH 05/13] Adding section on objects --- .../advanced-ada/parts/data_types/types.rst | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 3a5d7f8bb..be31939a0 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -236,10 +236,278 @@ In this example, we see instances of the following kinds of names: Objects ------- +The term *object* may be misleading for readers that have a strong background +in object-oriented programming. Moreover, its meaning can vary depending on the +context. Therefore, it's important to define what we mean by *objects* when +focusing on Ada programming. + +In computer science, the term :wikipedia:`object ` +can refer to a piece of data stored in memory |mdash| but it can also refer to +a table or a form in a database. Also, even when we define the term *object* as +data in memory, we can still classify programming languages as +:wikipedia:`object-based ` or +:wikipedia:`object-oriented ` languages. + +.. admonition:: Important + + In object-oriented programming, an object belongs to a *class* of objects. + In Ada, this kind of objects are called *tagged* objects. Note, however, + that we can have objects that don't belong to a class of objects: those are + called *untagged* objects. + +In the context of Ada programming, an object is an "entity that contains a +value, and is either a constant or a variable" |mdash| according to the Ada +Reference Manual. In other words, any constants or variables that we declare in +Ada source code are objects. In addition, there are other examples of objects +that don't originate from object declarations: + +.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Types.Objects.Object_Examples + + procedure Show_Objects is + + type New_Integer is new + Integer; + + type Integer_Array is + array (Positive range <>) of + Integer; + + procedure Dummy (Obj : Integer) + is null; + -- ^^^ + -- object + + task type TT is + entry Start (Id : Integer); + -- ^^ + -- object + end TT; + + task body TT is + begin + accept Start (Id : Integer) do + null; + end Start; + end TT; + + function Add_One (V : Integer) + -- ^ + -- view of an object + return Integer is + begin + return V + 1; + -- ^^^^^ + -- object + end Add_One; + + Arr : Integer_Array (1 .. 10); + -- ^^^^^^^^^^^^^^^ + -- object + + NI : New_Integer; + begin + Arr (1 .. 3) := (others => 1); + -- ^^^^^^^^ + -- object + -- ^^^^^^^^^^^^^ + -- object + + NI := New_Integer (Arr (1)); + -- ^^^^^^^^^^^^^^^^^^^^^ + -- object + + for I in Arr'Range loop + -- ^ + -- object + + Arr (I) := Add_One (Arr (I)); + -- ^^^^^^^ + -- object + end loop; + end Show_Objects; + +As we can see in this code example a formal parameter of a subprogram or an +entry is also an object |mdash| in addition, so are +:ref:`value conversions `, the result returned by a +function, the result of evaluating an :doc:`aggregate <./aggregates>`, loop +parameters, :doc:`arrays <./arrays>`, or the slices or the components of an +array. + +.. todo:: + + Add link to section on entries. + +.. todo:: + + Add link to section on array slices. + +Other examples of objects include: + +- the object created via a :ref:`view conversion `; + +- a :ref:`dereference ` of an + :ref:`access-to-variable ` value; + +- the return object of a function; + +- a choice parameter of an + :ref:`exception handler `. + .. admonition:: In the Ada Reference Manual - :arm22:`3.3 Objects and Named Numbers <3-3>` + +Constant and variable objects +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Objects can be classified as constant and variable objects. When declaring +objects, the distinction is clear: + +.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Types.Objects.Object_Declaration_Examples + + procedure Show_Objects is + Const : constant Integer := 42; + Var : Integer := 0; + begin + null; + end Show_Objects; + +In this example, :ada:`Const` is a constant object, while :ada:`Var` is a +variable object. + +In addition to this, constant objects include: + +- the :ref:`discriminant component ` of a + variable discriminant; + +- a formal parameter or generic formal object of mode :ada:`in`. + +.. todo:: + + Add link to section on generic formal object. + +On the other hand, variable objects include: + +- the object created via a :ref:`view conversion ` of + a variable; + +- a :ref:`dereference ` of an + :ref:`access-to-variable ` value. + +For example: + +.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Types.Objects.Object_Examples + + procedure Show_Objects is + + type Rec (Id : Positive) is + record + Value : Integer; + end record; + + type Rec_Access is + access all Rec; + + R : aliased Rec (99); + -- ^^ + -- Discriminant `Id` is a + -- constant object. + -- + -- `R` is a variable object, + -- though. + + R_Acc : Rec_Access := R'Access; + + procedure Process (R : Rec) is + null; + -- ^ + -- constant object + begin + R.Value := 0; + -- ^^^ + -- variable object + + R_Acc.all.Value := 1; + -- ^^^^^ + -- variable object + end Show_Objects; + +In this example, we see that :ada:`R` is a variable object, while its +:ada:`Id` discriminant is a constant object. In addition, the :ada:`R_Acc.all` +dereference is a variable object. Finally, the :ada:`in` parameter of procedure +:ada:`Process` is a constant object. + +.. admonition:: In the Ada Reference Manual + + - :arm22:`3.3 Objects and Named Numbers <3-3>` + - :arm22:`3.3.1 Object Declarations <3-3-1>` + + +View of an object +~~~~~~~~~~~~~~~~~ + +As we've just seen, an object can be either constant or variable. In addition, +the *view* of an object is classified as constant or variable as well. + +.. todo:: + + Discuss more about what the view of an object really means? + +Before we start, note that the classification of an object as (constant or +variable) doesn't directly imply how its view is classified. You may, for +example, expect that a constant object has a constant view, but this is not +necessarily the case, as we discuss in this section. + +A constant object only has a constant view if it doesn't have a part that has a +variable view |mdash| a part has a variable view if it is of +:ref:`immutably limited type `, +:ref:`controlled type `, +:ref:`private type `, or private extension. +If any of those parts exist in a constant object, then we say that the whole +object has a variable view *because* of those parts with variable view. +Otherwise, if a constant object doesn't have any parts with variable view, then +it has a constant view. + +.. todo:: + + Add link to section on private extension. + +In contrast, variable objects always have a variable view. + + +Named numbers +~~~~~~~~~~~~~ + +In addition to objects, we can have named numbers. Those aren't objects, but +rather names that we assign to numeric values. For example: + +.. todo:: + + Add link to section on names. + +.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Types.Objects.Named_Number + + procedure Show_Named_Number is + + Pi : constant := 3.1415926535; + + begin + null; + end Show_Named_Number; + +In this example, :ada:`Pi` is a named number. + +A named number is always known at compilation time. Also, it doesn't have a +type associated with it. In fact, its type is called universal real or +universal integer |mdash| depending on the number being a real or integer +number. + +.. admonition:: In the Ada Reference Manual + + - :arm22:`3.3.2 Number Declarations <3-3-2>` + + Scalar Types ------------ @@ -1881,6 +2149,8 @@ conversion, but only :ada:`A`. In a value conversion, we could use both forms. - :arm22:`4.6 Type Conversions <4-6>` +.. _Adv_Ada_Value_Conversion: + Value conversion ~~~~~~~~~~~~~~~~ From d92a373b4f950079c0b5ba2ae14f2585b21623f0 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:43:44 +0200 Subject: [PATCH 06/13] Editorial changes: improving description --- content/courses/advanced-ada/parts/data_types/types.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index be31939a0..7e852a241 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -251,7 +251,7 @@ data in memory, we can still classify programming languages as .. admonition:: Important In object-oriented programming, an object belongs to a *class* of objects. - In Ada, this kind of objects are called *tagged* objects. Note, however, + In Ada, objects of this kind are called *tagged* objects. Note, however, that we can have objects that don't belong to a class of objects: those are called *untagged* objects. @@ -330,8 +330,8 @@ As we can see in this code example a formal parameter of a subprogram or an entry is also an object |mdash| in addition, so are :ref:`value conversions `, the result returned by a function, the result of evaluating an :doc:`aggregate <./aggregates>`, loop -parameters, :doc:`arrays <./arrays>`, or the slices or the components of an -array. +parameters, :doc:`arrays <./arrays>`, or the slices of arrays objects, or the +components of composite objects. .. todo:: From 16fa4d092342d498b6a60b902db3b7aa346395a1 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:44:19 +0200 Subject: [PATCH 07/13] Editorial change: renaming types / objects in code example --- .../advanced-ada/parts/data_types/types.rst | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 7e852a241..3bae8b8ef 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -401,42 +401,42 @@ For example: procedure Show_Objects is - type Rec (Id : Positive) is + type Device (Id : Positive) is record Value : Integer; end record; - type Rec_Access is - access all Rec; + type Device_Access is + access all Device; - R : aliased Rec (99); - -- ^^ + Dev : aliased Device (99); + -- ^^ -- Discriminant `Id` is a -- constant object. -- - -- `R` is a variable object, + -- `Dev` is a variable object, -- though. - R_Acc : Rec_Access := R'Access; + Dev_Acc : Device_Access := Dev'Access; - procedure Process (R : Rec) is + procedure Process (D : Device) is null; -- ^ -- constant object begin - R.Value := 0; - -- ^^^ + Dev.Value := 0; + -- ^^^^^ -- variable object - R_Acc.all.Value := 1; - -- ^^^^^ + Dev_Acc.all.Value := 1; + -- ^^^^^^^ -- variable object end Show_Objects; -In this example, we see that :ada:`R` is a variable object, while its -:ada:`Id` discriminant is a constant object. In addition, the :ada:`R_Acc.all` -dereference is a variable object. Finally, the :ada:`in` parameter of procedure -:ada:`Process` is a constant object. +In this example, we see that :ada:`Dev` is a variable object, while its +:ada:`Id` discriminant is a constant object. In addition, the +:ada:`Dev_Acc.all` dereference is a variable object. Finally, the :ada:`in` +parameter of procedure :ada:`Process` is a constant object. .. admonition:: In the Ada Reference Manual From dee632c36ead467c5db9233c5cf53db7e156238c Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:45:34 +0200 Subject: [PATCH 08/13] Editorial change: removing parentheses --- content/courses/advanced-ada/parts/data_types/types.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 3bae8b8ef..9738b9d47 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -454,8 +454,8 @@ the *view* of an object is classified as constant or variable as well. Discuss more about what the view of an object really means? -Before we start, note that the classification of an object as (constant or -variable) doesn't directly imply how its view is classified. You may, for +Before we start, note that the classification of an object as constant or +variable doesn't directly imply how its view is classified. You may, for example, expect that a constant object has a constant view, but this is not necessarily the case, as we discuss in this section. From 4341276015fca10993f939f665be1b83253afab4 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:45:46 +0200 Subject: [PATCH 09/13] Improving description of object views --- .../advanced-ada/parts/data_types/types.rst | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 9738b9d47..dab0e7667 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -457,17 +457,18 @@ the *view* of an object is classified as constant or variable as well. Before we start, note that the classification of an object as constant or variable doesn't directly imply how its view is classified. You may, for example, expect that a constant object has a constant view, but this is not -necessarily the case, as we discuss in this section. +necessarily the case, as we discuss in this section. (In fact, a constant +object only has a constant view if it doesn't have a part that has a variable +view.) -A constant object only has a constant view if it doesn't have a part that has a -variable view |mdash| a part has a variable view if it is of +A part of an object has a variable view if it is of :ref:`immutably limited type `, :ref:`controlled type `, :ref:`private type `, or private extension. -If any of those parts exist in a constant object, then we say that the whole -object has a variable view *because* of those parts with variable view. -Otherwise, if a constant object doesn't have any parts with variable view, then -it has a constant view. +In that sense, if any of those parts with variable view exist in a constant +object, then we say that the *whole object* has a variable view. +Only if a constant object doesn't have *any* parts with variable view, then +this object has a constant view. .. todo:: From f244a3601c687fd3774506ecbcc2b54c1432533a Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:46:22 +0200 Subject: [PATCH 10/13] Adding simple code example for object view --- .../advanced-ada/parts/data_types/types.rst | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index dab0e7667..5ef9c1f92 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -476,6 +476,59 @@ this object has a constant view. In contrast, variable objects always have a variable view. +Let's see an example: + +.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Types.Objects.Object_View + + package Devices is + + type Device_Settings is + record + Started : Boolean; + end record; + + type Device (Id : Positive) is + private; + + function Init (Id : Positive) + return Device; + + private + + type Device (Id : Positive) is + null record; + + function Init (Id : Positive) + return Device is + (Device'(Id => Id)); + + end Devices; + + + with Devices; use Devices; + + procedure Show_Object_View is + Dev : constant Device := Init (5); + -- Constant object with + -- variable view. + + Default : constant Device_Settings + := (Started => False); + -- Constant object with + -- constant view. + + Settings : Device_Settings; + + begin + Settings := (Started => True); + end Show_Object_View; + +In this example, both :ada:`Default_S` and :ada:`Dev` are constant objects. +However, they have different views: while :ada:`Default_S` has a constant view +because it doesn't have any parts with variable view, :ada:`Dev` has a variable +view because it's a private type. Finally, as expected, :ada:`Settings` has a +variable view because it's a variable object. + Named numbers ~~~~~~~~~~~~~ From 1ce501a67de0ed71da6dfdfeefe354b5eeb41db6 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Sat, 5 Apr 2025 02:46:49 +0200 Subject: [PATCH 11/13] Editorial change: adding reference to section on names --- content/courses/advanced-ada/parts/data_types/types.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 5ef9c1f92..a5a6f2842 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -534,7 +534,8 @@ Named numbers ~~~~~~~~~~~~~ In addition to objects, we can have named numbers. Those aren't objects, but -rather names that we assign to numeric values. For example: +rather :ref:`names ` that we assign to numeric values. For +example: .. todo:: From 90bc56a4706906e67584c3acd4f414f4b3a653f1 Mon Sep 17 00:00:00 2001 From: gusthoff Date: Fri, 11 Apr 2025 16:02:19 +0200 Subject: [PATCH 12/13] Editorial change: adding todo item --- .../advanced-ada/parts/data_types/numerics.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/content/courses/advanced-ada/parts/data_types/numerics.rst b/content/courses/advanced-ada/parts/data_types/numerics.rst index 94e9b6b72..510c4f703 100644 --- a/content/courses/advanced-ada/parts/data_types/numerics.rst +++ b/content/courses/advanced-ada/parts/data_types/numerics.rst @@ -3,6 +3,24 @@ Numerics .. include:: ../../../global.txt +.. :: + + .. _Adv_Ada_Universal_Real_Integer: + + Universal Real and Integer + -------------------------- + + .. todo:: + + Complete section! + + - Universal real vs. integer + - Accuracy + - Compile time vs. runtime evaluation + - Named Numbers + - Operations in named numbers expressions + + Modular Types ------------- From c79a3fc8ddbb8cf754611223ac8721c343c62c3d Mon Sep 17 00:00:00 2001 From: gusthoff Date: Fri, 11 Apr 2025 16:08:09 +0200 Subject: [PATCH 13/13] Mention section on universal real and integer numbers --- content/courses/advanced-ada/parts/data_types/types.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index a5a6f2842..5c4b786cd 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -556,7 +556,13 @@ In this example, :ada:`Pi` is a named number. A named number is always known at compilation time. Also, it doesn't have a type associated with it. In fact, its type is called universal real or universal integer |mdash| depending on the number being a real or integer -number. +number. (In this specific case, :ada:`Pi` is a universal real number.) We talk +about universal real and universal integer types in another chapter. + +.. todo:: + + Add link to section on universal real and universal integer types + (Adv_Ada_Universal_Real_Integer) once it's available. .. admonition:: In the Ada Reference Manual