From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,25c6c17a48209275 X-Google-Attributes: gid103376,public From: Brian Rogoff Subject: Re: Generics question Date: 1997/07/23 Message-ID: #1/1 X-Deja-AN: 258452250 References: <5r37k9$h4p$1@kelp.mbay.net> Newsgroups: comp.lang.ada Date: 1997-07-23T00:00:00+00:00 List-Id: 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