Albatros23
Albatros23

Reputation: 331

Ada: private child packages and visibility of elements from parent packages

Having the following package hierarchy:

limited private with Root.Child;

package Root is
  
  type Root_t is tagged private;

  procedure p_dispatcher (this : in out Root_t);

private

  type ChildWideAcc_t is access Root.Child.Child_t;
  
  type Root_t is tagged 
    record
      ref : ChildWideAcc_t;
    end record;

end Root;
private package Root.Child is

  type Child_t is abstract tagged private;

  procedure p_opExample (
    this    : in out Child_t;
    Context : in out Root.Root_t
  );

private

  type Child_t is abstract tagged null record;

end Root.Child;

I understand that a child package with's automatically it's parent's spec, and a private child package the private part also, but if I do not write any "use" statement on the child I can write too "Root_t" for the "Context" argument type for the procedure "p_opExample" and my program is running exactly the same, like this:

  procedure p_opExample (
    this    : in out Child_t;
    Context : in out Root_t
  );

Is this behaviour ok? Why?

Upvotes: 1

Views: 649

Answers (1)

Jim Rogers
Jim Rogers

Reputation: 5021

Since Ada83 it has been possible to nest package definitions within package definitions. An example of nested package structure is the package Ada.Text_IO.

Ada95 introduced the ability to define child packages, which are logically the same as nested packages but are written in a separate file from the parent package. Since they are logically the same the public part of a child package is logically nested within the public part of its parent package and has direct visibility to all the entities declared in the public part of the parent package. The private part of a child package is logically nested in the private part of the parent package and has direct visibility to all the entities declared or defined within the both the public and private parts of the parent package. The child package body is logically nested within the parent package body and has direct visibility to all entities defined in the parent package public region, the parent package private region and the parent package body.

It is therefore not necessary to append the parent package name to an entity defined in the parent package and used in a child of that parent package.

The shared visibility of parent and child packages is not like visibility acquired through the use of a context clause (with and use). A context clause only provides visibility to the public part of a package.

Upvotes: 1

Related Questions