comp.lang.ada
 help / color / mirror / Atom feed
* Re: Novice Question On Generics
@ 1992-09-16  0:17 agate!linus!think.com!yale.edu!spool.mu.edu!caen!uakari.primate.wisc.edu!
  0 siblings, 0 replies; 2+ messages in thread
From: agate!linus!think.com!yale.edu!spool.mu.edu!caen!uakari.primate.wisc.edu! @ 1992-09-16  0:17 UTC (permalink / raw)


In <1992Sep15.184345.12197@saifr00.cfsat.honeywell.com> lam@saifr00.cfsat.honey
well.com (Josh Lam) writes:

>I have a generic package Queue that has the function enqueue.

>This generic package is 'with' into two packages A and B that looks
>like:

>with Queue                                with Queue
>package A  is                             package B is 

> ...                                      ...

>Question:
>Is there a way to write a generic procedure Run_Proc that the two packages 
>A and B can use?   Note that the user *does not* need to know my_QA 

generic
  type Queue_Item is private;
  with procedure Enqueue(Item : in Queue_Item);
procedure Run_Proc is
begin
  Enqueue(Queue_Item);
end Run_Proc;

with Run_Proc;                           with Run_Proc;
with Queue;                              with Queue;
package A is                             package B is
  type A_Rec is record...                  type B_Rec is record...
  end record;                              end record;
  
  package A_Queue is new Queue(A_Rec);     package B_Queue is new
                                             Queue(B_Rec);

  procedure A_Run_Proc is new              package B_Run_Proc is new
    Run_Proc(A_Rec, A_Queue.Enqueue);        Run_Proc(B_Rec,B_Queue.Enqueue);

  ...                                      ...

end A;                                   end B;

BUT it is a pain *if* Run_Proc uses many procedures from
Queue.  Each routine from the queue package needs to be
a generic parameter to the Run_Proc (generic) routine.

In Ada 9X, you will be able to instantiate a routine or
package with an instantiation of another generic package
which will make the whole idea of generics much better.
So you will be able to to something like this:

generic
  with Some_Instantion_Of the generic Queue package; -- I dunno the syntax
procedure Run_Proc(Item : Some_Instantiation_Of.Item) is
begin
  SIO.Enqueue(Item);
  SIO.Dequeue(Item);
  SIO.Do_Anything_in_the_generic_package(Item);
end;

--Thor
collard@capsrv.jhuapl.edu
dlc@ddsdx2.jhuapl.edu

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

* Re: Novice Question On Generics
@ 1992-09-16  5:24 Andrew Dunstan
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Dunstan @ 1992-09-16  5:24 UTC (permalink / raw)


In article <1992Sep15.184345.12197@saifr00.cfsat.honeywell.com>, lam@saifr00.cf
sat.honeywell.com (Josh Lam) writes:
[sorry for long repost - I'v cut it down a fair bit]
|> with Queue                                with Queue
|> package A  is                             package B is 
|>  ...                                      ...
|>  type A_record is                           type B_record is
|>   record                                     record
|>    ...                                         ...
|>   end record                                 end record
|>  package my_QA is new Queue(A_record);       package my_QB is new Queue(B_re
cord);
|>  ...                                          ...
|>  Run_Proc(X : A_record);                      Run_Proc(Y : B_record);
|>  ...                                          ...
|> end A;                                    end B;
|> 
|> Now the Run_Proc procedures for both packages will look like:
|> 
|> Run_Proc(X : A_record) is                 Run_Proc(Y : B_record) is
|> begin                                     begin
|>   my_QA.enqueue(X);                         my_QB.enqueue(Y);
|> end Run_Proc;                              end Run_Proc;
|> 
|> Basically, they contain the same functionality.  In this example, I only
|> wrote one line of code for simplicity of illustration, in reality, there
|> could be more lines of code but same functionality.  The only difference
|> is the queue instantiations my_QA and my_QB.  
|> 
|> Question:
|> Is there a way to write a generic procedure Run_Proc that the two packages 
|> A and B can use?   Note that the user *does not* need to know my_QA 
|> and my_QB ie they are private members to A and B respectively and Run_Proc i
s 
|> the only interface to the outside world. ie, The user only need to type 
|> A.Run_Proc(X) or B.Run_Proc(Y).


I think this is what you want:

with queue;
generic
   type x is private;
package run_proc_pack is
   -- following line might be in the body, if queue not needed to be public
   package rpq is new queue(x);
   procedure run_proc(a : x);
end run_proc_pack;
package body run_proc_pack is
   procedure run_proc(a: x) is
   begin
      rpq.enqueue(a);
   end run_proc;
end run_proc_pack;

...

package a_run is new run_proc_pack(a_record);
package b_run is new run_proc_pack(b_record);

...

a_run.run_proc(a_rec);
...


without knowing more about what you are actually trying to do, it is
hard to tell if this is what you really want.


-- 
#######################################################################
#  Andrew Dunstan                   #   There's nothing good or bad   #
#  Department of Computer Science   #   but thinking makes it so.     #
#  University of Adelaide           #                                 #
#  South Australia                  #          - Shakespeare          #
#  net: andrewd@cs.adelaide.edu.au  #                                 #
#######################################################################

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

end of thread, other threads:[~1992-09-16  5:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-09-16  5:24 Novice Question On Generics Andrew Dunstan
  -- strict thread matches above, loose matches on Subject: below --
1992-09-16  0:17 agate!linus!think.com!yale.edu!spool.mu.edu!caen!uakari.primate.wisc.edu!

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