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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Understanding generic package functions Date: Mon, 2 Nov 2015 20:36:41 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Tue, 3 Nov 2015 03:34:30 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="caa759af2a9c666aec02942f6fe5abd6"; logging-data="22774"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mo1AvnPMB5UJ6S76XoM0JLDUNXFFYShc=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: Cancel-Lock: sha1:4fFwsbnH02jPgcBfOJc7AZ00sP8= X-Enigmail-Draft-Status: N1110 Xref: news.eternal-september.org comp.lang.ada:28185 Date: 2015-11-02T20:36:41-07:00 List-Id: On 11/02/2015 05:45 PM, Nick Gordon wrote: > > My thought is that I have to create a new instance of the > Generic_Elementary_Functions package, but I'm not sure if that's correct, or > how to do that. In general, I'm noticing that the learning curve for Ada is > steep, and there are not terribly many "quick references" for the language. Generics exist for reuse. For example, Ada.Numerics.Generic_Elementary_Functions allows the reuse of its algorithms with numerous floating-point types. You create an instance of the generic package for a specific floating-point types through the process of instantiation. You can think of a generic as a template and instantiation in terms of macro expansion; you end up with a package with the name of the instantiation and every occurrence of the generic formal parameters replaced with the corresponding actual parameters from the instantiation. (This isn't exactly correct but is close enough for most people, especially for a beginner.) For example: package Math is new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Float); Now you have a package named Math with functions that operate on type Float: F : Float := Math.Sqrt (Ada.Numerics.e); If you define your own floating-point type type Big is digits System.Max_Digits; you can instantiate Generic_Elementary_Functions for it, too package Big_Math is new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Big); -- Jeff Carter "[T]he [Agile] idea that it's bad to spend an appropriate time at the beginning of the project to clarify the overall requirements and design is nonsense." Bertrand Meyer 149