Legacy features

Nested packages

Nested packages, as the name suggests, are declared within a parent package. This contrasts with child packages, which are declared independently. For example, this would be a nested package for the Week package:

package Week is Mon : constant String := "Monday"; Tue : constant String := "Tuesday"; Wed : constant String := "Wednesday"; Thu : constant String := "Thursday"; Fri : constant String := "Friday"; Sat : constant String := "Saturday"; Sun : constant String := "Sunday"; package Nested is function Get_First_Of_Week return String; end Nested; end Week;

In contrast to child packages, we don't write package body Week.Nested is ... to implement the package body of Nested. Instead, the package body of Nested is nested in the package body of the parent package Week:

package body Week is package body Nested is function Get_First_Of_Week return String is begin return Mon; end Get_First_Of_Week; end Nested; end Week;

We can now use elements from Week.Nested in a test application:

with Ada.Text_IO; use Ada.Text_IO; with Week; procedure Main is use Week.Nested; begin Put_Line ("First day of the week is " & Get_First_Of_Week); end Main;

Note that we cannot access the Week.Nested package directly using with Week.Nested because Nested is actually part of Week, not a child package. We can, however, still write use Week.Nested — as we did in the example above.

Visibility

Let's now discuss visibility of nested packages. Because the body of nested packages is part of the body of their parent, nested packages have the same visibility as their parent package. Let's rewrite the previous example using nested packages to illustrate this:

package Book is Title : constant String := "Visible for my children"; function Get_Title return String; function Get_Author return String; package Additional_Operations is function Get_Extended_Title return String; function Get_Extended_Author return String; end Additional_Operations; end Book;

Now, because Author is declared before the body of the nested package Additional_Operations, we can use it in the implementation of the Get_Extended_Author function:

package body Book is Author : constant String := "Author not visible for my children"; function Get_Title return String is begin return Title; end Get_Title; function Get_Author return String is begin return Author; end Get_Author; package body Additional_Operations is function Get_Extended_Title return String is begin return "Book Title: " & Title; end Get_Extended_Title; function Get_Extended_Author return String is begin return "Book Author: " & Author; end Get_Extended_Author; end Additional_Operations; end Book;

This is the test application in this case:

with Ada.Text_IO; use Ada.Text_IO; with Book; procedure Main is use Book.Additional_Operations; begin Put_Line (Get_Extended_Title); Put_Line (Get_Extended_Author); end Main;

separate compilation

Todo

Complete section!