From: adam@irvine.com (Adam Beneschan)
Newsgroups: comp.lang.ada
Subject: Re: Generic Package Visibility Problems! (help)
Date: 4 Oct 92 21:02:05 GMT
Distribution: comp
Organization: Irvine Compiler Corp., Irvine, California, USA



From Mark Young (young@gdfwc3):

> I am having visibility problems with generic packages.
> 
> Here's an example:
> 
> file #1
> -----------------------------------------------
> generic 
> 
>   Generic_Parameter : Generic_Parameter_Type;
> 
> package Color is
> 
>   type RGB_Type is (RED,GREEN,BLUE);
> 
>   function Get_Color return RGB_Type;
> 
> end Color;
> ----------------------------------------------
> file #2
> ----------------------------------------------
> with Color;
> 
> package Caller is
> 
>   package C_1 is new Caller( Generic_Argument );
> 
>   procedure Test_Color;
> 
> end Caller;
> 
> package body Caller is 
> 
>   procedure Test_Color is separate;
> 
> end Caller;
> ---------------------------------------------
> file #3
> ---------------------------------------------
> separate( Caller )
> 
> procedure Test_Color is
> 
> begin
> 
>   if ( C_1.Get_Color = C_1.RED ) then
> 	               -------
> 	null;
> 
>   end if;
> 
> end Test_Color;
> ----------------------------------------------
> 
> This will not work.  For some reason the compiler cannot
> resolve C_1.RED.  It tells me that = is not a legal 
> operation, that C_1.Get_Color returns type RGB_Type but, 
> that it does not understand C_1.RED and that perhaps I 
> needed a "use" clause in the body of Caller.  I don't
> understand this.  My understanding of Ada scoping would
> lead me to believe that this is correct.  Am I missing 
> something?  
> 
> BTW: this is not a complete example, it is only to illustrate
> a point.
> 
> Thanks,
> 
> Mark Young
> 


First of all, I'm assuming that the following line:
      
      package C_1 is new Caller( Generic_Argument );

is really:
      
      package C_1 is new Color( Generic_Argument );

and the line was mistyped when you posted the news.  If this is the case,
then the only problem is that the operator "=" is not visible.  When you
define a new type in a package specification, Ada automatically defines
new operators for you (such as "=", "+", "<", depending on what kind of
type it is).  However, those operators are directly visible only if you "USE" 
the package, just the same as any procedures or functions you may have defined
in the package specification.  For example:

     package P is
         type NEW_INT is new integer;
     end P;

     with P;
     procedure Q is
         x, y : P.NEW_INT;
     begin
         x := 3;
         y := x + 10;    -- WRONG!!!
     end Q;
     
The compiler will give you an error message because "+" is not visible at that
point.  This is the same problem you're having with the "=" operator.  This
has absolutely nothing to do with the fact that your package is generic.

You can solve your problem in one of three ways:

1)  Explicitly qualify the "=" operator, i.e.:
    
    if C_1."=" (C_1.Get_Color, C_1.RED) then  . . .

2)  Add the statement:

    use C_1;

    This will make the = operator visible.  You can put the USE statement in
    the specification of Caller after you declare C_1;  in the package body
    of Caller before the declaration of Test_Color;  or in the separate
    procedure body, between "procedure Test_Color is" and "begin".  

3)  Use RENAMES to make "=" visible:

    separate (Caller)
    procedure Test_Color is
       function "=" (left, right : C_1.RGB_Type) return boolean
          renames C_1."=";
    begin
       if C_1.Get_Color = C_1.RED then . . .
    end Test_Color;


LRM references: 3.3.3(2), 4.5(6), 8.3(18)
-- 
-----------------------------------------------------------------------------
  Adam Beneschan                     voice: (714) 250-1368  ext. 204
  Irvine Compiler Corp.              fax:   (714) 250-0676
  34 Executive Park, Suite 270       email: abeneschan@irvine.com