comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@shellx.best.com>
Subject: Re: Generics question
Date: 1997/07/23
Date: 1997-07-23T00:00:00+00:00	[thread overview]
Message-ID: <Pine.SGI.3.95.970723191547.6238A-100000@shellx.best.com> (raw)
In-Reply-To: 5r37k9$h4p$1@kelp.mbay.net


On 22 Jul 1997, Skip Carter wrote:
> 	I have a computational algorithm that I've implemented that has
> 	the (admitedly unusual) property that it is perfectly valid for either
> 	arrays of Integers or arrays of Floats.    As far as I can see I
> 	CANNOT make a generic version of the package that will work for
> 	both, e.g.:

To do a general numerical algorithm like you describe the Ada 95 idiom is: 

generic 
	type Numeric_Type is private;
	with function "+" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	with function "-" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	with function "*" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	... etc ...
package Numerics is 
-- no body
end;

generic 
	with package Numbers is new Numerics(<>); 
        -- generic formal package parameters
package My_Numerical_Algorithms is 
... insert your stuff here ...
end;

I suppose someone will suggest making the numbers tagged types too, but I 
doubt you want to dispatch and if you make it a tagged type you get
probably double the size of your matrices. 

> 	Is there a way to do this ?   or do I have to actually make two different copies
> 	(which differ only in the definition of ELEMENT).

Note that what I describe easily handles any numeric type you have, with
the caveat that if you end up heap allocating numbers (say bignums) you 
will probably get yourself into trouble unless you deal with finalization.
But Integers, Floats, Complex, Interval, all work fine.

-- Brian






  parent reply	other threads:[~1997-07-23  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-07-22  0:00 Generics question Skip Carter
1997-07-22  0:00 ` W. Wesley Groleau x4923
1997-07-23  0:00 ` Robert Dewar
1997-07-23  0:00 ` Brian Rogoff [this message]
1997-07-24  0:00   ` Michael F Brenner
1997-07-24  0:00     ` Brian Rogoff
1997-07-24  0:00     ` Jon S Anthony
  -- strict thread matches above, loose matches on Subject: below --
1989-09-19 23:03 Generics Question Jay Labhart
replies disabled

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