comp.lang.ada
 help / color / mirror / Atom feed
From: stt@spock.camb.inmet.com (Tucker Taft)
Subject: Re: generics using generics (long)
Date: Wed, 7 Sep 1994 16:54:53 GMT
Date: 1994-09-07T16:54:53+00:00	[thread overview]
Message-ID: <CvrqzI.ILr@inmet.camb.inmet.com> (raw)
In-Reply-To: CvqEyG.nD@taurus.cs.nps.navy.mil

In article <CvqEyG.nD@taurus.cs.nps.navy.mil>,
Skip Carter <skip@taygeta.oc.nps.navy.mil> wrote:

>	I am trying to figure out how to design some generic code to work
>	with other generics as their parameters but can't figure out a clean way
>	to do it.
>
>	Here is what I am trying to do:
>
>
>-- A Generic 1-D floating point array type
>generic
>RealNumber is digits<>;
 ^^ type
>
>Package G1DArray is
>	type 1DArray is array(INTEGER range<>) of RealNumber;
>	type 1DArrayHandle is access 1DArrray;
>
>	-- then a bunch of procedure specifications 
>       -- for I/O and reallocating the array on the fly,
>	-- the usual kind of stuff...
>
>end G1DArray;
>
>
>-- A Generic Package for fast Hartley transforms, 
>-- intended to work with any float type
>with G1DArray;
>
>generic
>Type RealNumber is digits<>;
>Type FHT_Array  is new G1DArray(RealNumber);         -- this is wrong, 
>                                             -- but it is the kind of thing
>				                   -- that I have in mind

In Ada 9X, you can import an instance of a generic.  This would
allow you to replace both of the above formal parameters with
the single "formal package" parameter:

   with package FHT is new G1DArray(<>);
   use FHT;  -- This makes FHT.1DArray and FHT.1DArrayHandle directly visible

>Package Hartley is
>		procedure Normalize(fx : 1DArrayHandle);
>		procedure FHT( fx : 1DArrayHandle );
>end Hartley;

You instantiate Hartley by first instantiating G1DArray,
and then passing that instance in an instantiation of Hartley.
For example:

    package Float_Vector is new G1DArray(Float);
    package Float_Hartley is new Hartley(Float_Vector);

>So an application would look like:
>
>with Hartley, G1DArray
>
>procedure FHTMAIN is
>	type Real is new long_float;
>
>	package Real_Vector  is new G1DArray( Real );
>
>	package Real_Hartley is new Hartley(Real,Real_Vector);   
>                                  -- this is how I'd LIKE to be
>                                 -- able to instantiate the package

As indicated above, if you use a formal package parameter,
this instantiation would be:

        package Real_Hartley is new Hartley(Real_Vector);

>	fx    : 1DArrayHandle;
>
>
>	-- ...etc...
>
>	-- code to do lots of things with the 1D Array fx, 
>       -- among them calculating
>	-- its hartley transform
>
>end FHTMAIN;

> ...
>	How do I specify things to work the other way around ?

Formal packages were provided in Ada 9X to solve this problem.

>	Is there is good book out there that teaches Ada idioms, 
>       as opposed to grammar,
>	that can help with this kind of question ?

There are a number of good Ada books that include examples
of using Ada.  See the comp.lang.ada FAQ (on ajpo.sei.cmu.edu
and the EPFL WWW server).  Some of them already cover Ada 9X
features (e.g. John Barnes 4th edition of "Programming in Ada").

> Everett (Skip) Carter        Phone:  408-656-3318 FAX: 408-656-2712
> Naval Postgraduate School    INTERNET: skip@taygeta.oc.nps.navy.mil

-Tucker Taft   stt@inmet.com



      parent reply	other threads:[~1994-09-07 16:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CvqEyG.nD@taurus.cs.nps.navy.mil>
1994-09-07  9:38 ` generics using generics (long) Robert I. Eachus
1994-09-07 16:54 ` Tucker Taft [this message]
replies disabled

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