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>` 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 ------------- diff --git a/content/courses/advanced-ada/parts/data_types/types.rst b/content/courses/advanced-ada/parts/data_types/types.rst index 7d35f9ec3..5c4b786cd 100644 --- a/content/courses/advanced-ada/parts/data_types/types.rst +++ b/content/courses/advanced-ada/parts/data_types/types.rst @@ -231,16 +231,342 @@ 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: +.. _Adv_Ada_Objects: - Objects - ------- +Objects +------- - .. todo:: +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. - - Definition of an object in Ada - - Difference to object-oriented 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, 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. + +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 of arrays objects, or the +components of composite objects. + +.. 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 Device (Id : Positive) is + record + Value : Integer; + end record; + + type Device_Access is + access all Device; + + Dev : aliased Device (99); + -- ^^ + -- Discriminant `Id` is a + -- constant object. + -- + -- `Dev` is a variable object, + -- though. + + Dev_Acc : Device_Access := Dev'Access; + + procedure Process (D : Device) is + null; + -- ^ + -- constant object + begin + Dev.Value := 0; + -- ^^^^^ + -- variable object + + Dev_Acc.all.Value := 1; + -- ^^^^^^^ + -- variable object + end Show_Objects; + +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 + + - :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. (In fact, a constant +object only has a constant view if it doesn't have a part that has a variable +view.) + +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. +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:: + + Add link to section on private extension. + +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 +~~~~~~~~~~~~~ + +In addition to objects, we can have named numbers. Those aren't objects, but +rather :ref:`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. (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 + + - :arm22:`3.3.2 Number Declarations <3-3-2>` Scalar Types @@ -1884,6 +2210,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 ~~~~~~~~~~~~~~~~ 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 ---------------------