comp.lang.ada
 help / color / mirror / Atom feed
* Generic Package Visibility Problems! (help)
@ 1992-10-02 18:35 Mark Young
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Young @ 1992-10-02 18:35 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Generic Package Visibility Problems! (help)
@ 1992-10-02 23:25 dog.ee.lbl.gov!overload.lbl.gov!agate!spool.mu.edu!nigel.msen.com!caen!de
  0 siblings, 0 replies; 4+ messages in thread
From: dog.ee.lbl.gov!overload.lbl.gov!agate!spool.mu.edu!nigel.msen.com!caen!de @ 1992-10-02 23:25 UTC (permalink / raw)


young@gdfwc3 (Mark Young) writes:
: 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


Well Mark,

	This won't work for the following reasons:

	1) You haven't used the generic formal in your generic package.
	2) You have passed to the generic formal a very odd data type.
	3) You are using a function in the body of TEST_COLOR wrong.

	The following corrected program does what you want to do.

-- ++
-- file #1
-- -- 
generic 
   type GENERIC_PARAMETER_TYPE is (<>);
package COLOR is
   RGB_TYPE : GENERIC_PARAMETER_TYPE;
   function GET_COLOR return GENERIC_PARAMETER_TYPE;
end COLOR;
-- ++
-- file #2
-- --
with COLOR;
package CALLER is
   type ZIPPY is ( RED, GREEN, BLUE );
   package C_1 is new COLOR( ZIPPY );
   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 ( RED = C_1.GET_COLOR ) THEN
      null;
  end if;
end TEST_COLOR;

============================
Hope this helps!

Richard Wallace
Digital Equipment Corporation
301 Rockrimmon Blvd. South
CXO2-1/7A
Colorado Springs, CO 80919-2398
(719)548-2792
<wallace@cookie.enet.dec.com>

	"The opinions expressed are my own, Uncle Ken or Uncle Bob
	 may, or may not, agree with me."

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generic Package Visibility Problems! (help)
@ 1992-10-04 21:02 Adam Beneschan
  0 siblings, 0 replies; 4+ messages in thread
From: Adam Beneschan @ 1992-10-04 21:02 UTC (permalink / raw)


>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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generic Package Visibility Problems! (help)
@ 1992-10-04 22:10 DONALD PRINCIOTTA
  0 siblings, 0 replies; 4+ messages in thread
From: DONALD PRINCIOTTA @ 1992-10-04 22:10 UTC (permalink / raw)


young@gdfwc3 (Mark Young) writes:


>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


--- START OF RESPONSE -------------------------------------------------------

Mark,
  The answer to your problem is simple... Just a plain old
everyday visibility problem.  I won't comment of the worth
of your test program, lets just say it could use a little
imagination and leave it at that.... 

The error message displayed by your compile was exactly right...
You don't have an equality operation visible at the point within
your program where you are attempting to invoke the function.

Think on where a types operations are visible??? The operations
are visible where the type is defined (i.e. inside of the 
package in this case.  If you did use the "use clause" it would
solve your visibility problem but that would be the LAMO way to
solve the problem.  You do in have visibility to the "=" operation
the problem stems from the fact that you don't have direct visibility
the the operation.  The following two examples demonstrate 
possible ways to get at your "=" operation.

separate( Caller )
procedure Test_Color is
begin
  if C_1."="(C_1.Get_Color,C_1.RED) then  -- "=" exists within C_1
       null;
  end if;
end Test_Color;

OR --------------------------------



separate( Caller )
procedure Test_Color is
   function "=" ( Left  : in C1.RGB_Type
                  Right : in C1.RGB_Type ) return Boolean renames C_1."=";
   -- Make only the "=" operation directly visible.
   -- This renaming clause could als0 have been placed inside of
   -- the parent routine Caller
begin
  if ( C_1.Get_Color = C_1.RED) then  -- "=" exists within C_1
       null;
  end if;
end Test_Color;


Hope this answers your question .................

                                Don Princiotta
                                dep@swlvx6.msd.ray.com

--- END OF RESPONSE ---------------------------------------------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1992-10-04 22:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-10-04 22:10 Generic Package Visibility Problems! (help) DONALD PRINCIOTTA
  -- strict thread matches above, loose matches on Subject: below --
1992-10-04 21:02 Adam Beneschan
1992-10-02 23:25 dog.ee.lbl.gov!overload.lbl.gov!agate!spool.mu.edu!nigel.msen.com!caen!de
1992-10-02 18:35 Mark Young

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox