[comp.lang.ada] Using the Use Clause to Limit Direct Visibility

dyer@ajpo.sei.cmu.edu (Richard Dye) (08/30/90)

There have recently been several discussions of the use clause in this
newsgroup.  On my project, we build our types packages with an
embedded operators package to provide limited direct visibility to the
infix operators of the types package.  Here's an example:

with Constants;
package Types is
   type Integer_Type is new Integer
      range Constants.Min .. Constants.Max;
   -- etc for other types
   package Operators is
      function "+"(L,R : Integer_Type) return Integer_Type 
         renames "+";
      -- etc for the other infix operators
   end Operators;
end Types;

Every package or subpragram which needs visibility to the Types
packages withs it in.  Every package or subprogram which needs direct
visibility to the infix operators on the Types package does a use on
Types.Operators.  

There is a subtlety to the use clause in this case.  In general, the
use clause can be either a context clause (occurs before the package
or subprogram specification) or a basic declaration (occurs after the
package or subprogram specification).  When the use clause is a
context clause, it can only refer to a simple_name (see LRM
10.1.1(3)).  This means that a use clause which is a context clause
can not say "use Types.Operators;" Only a use clause which is a basic
declaration can say "use Types.Operators;" because a use clause which
is a basic declaration can refer to a name (LRM 8.4(2)) which can be a
selected component (LRM 4.1(2)).

I mention this because we generated a multitude of strange syntax
errors until we moved our use clauses to a position after the
specifications.  We were unable to locate this difference in the Ada
semantics until we talked with Geoff Mendal of Stanford.  (Our thanks
to Geoff.)  Hopefully this note will help others limit direct
visibility to only those portions of a package which warrant direct
visibility.

Dick Dye
CTA, Inc.
M/S N8910
National Test Bed
Falcon AFB, CO 80912-5000
(719) 380-2578
dyer@ajpo.sei.cmu.edu