This is part of a series on programming in the Ada language, a mission critical programming language that I am attempting to learn and become proficient in.

It’s been another couple of days. To be honest, I haven’t done much with Ada in these recent days but here are some more findings as a result of my experimentation and research.

Last time, we had packages. Now, it’s time to deal with some child packages which are exactly what they sound like — a sub-package of a parent package. All of the standard packages are child packages of the “Ada” package. GNAT also provides some child packages under the “GNAT” package.

package Parent is
   -- enter some stuff here...
end Parent;

package Parent.Child is
   -- enter some more stuff here...
end Parent.Child;

Besides the obvious benefit of organization, child packages also provide some inheritance benefits. For example, every child package automatically has access to everything in the parent package. Normally, that requires a “with” statement at the top. Therefore, the following is redundant:

with Parent;
package Parent.Child is
   -- some code that uses functions in "Parent"
end Parent.Child;

An even bigger benefit is the ability for child packages to access the private declarations of the parent package. For example, if a parent is declared as:

package Parent is
   -- public declarations
   procedure Public_Foo;
private
   -- private declarations
   procedure Private_Foo;
end Parent;

then any other package can call Public_Foo (after using a with statement as shown above). However, only the package itself and all child packages can use Private_Foo.

Sometimes, it’s useful to have private child packages.

private package Parent.Private_Child is
   -- declarations
end Parent.Private_Child;

The declarations in such private packages are only accessible to other private units such in the bodies of sibling packages (ie. other child packages of the same parent).

Next time, it’s time to take a look at Ada’s highly sophisticated type system — effectively, the system that makes the language what it is.

Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>