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!feeder.eternal-september.org!news.glorb.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Understanding generic package functions References: Date: Tue, 03 Nov 2015 03:40:32 -0600 Message-ID: <86io5j2zfz.fsf@stephe-leake.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (windows-nt) Cancel-Lock: sha1:6r+IIxDjweJbyL2L43YKztWfVvQ= MIME-Version: 1.0 Content-Type: text/plain X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 73e4c56388117e97f808404675 X-Received-Bytes: 2223 X-Received-Body-CRC: 751162225 Xref: news.eternal-september.org comp.lang.ada:28195 Date: 2015-11-03T03:40:32-06:00 List-Id: Nick Gordon writes: > Okay, basically I'm writing a demo of Ada for a class, and I'm trying > to showcase Ada's math functions. Is this a class on Ada, or something else? > This is the code: > > function Recoded_Exp(Power: in Float) return Float is -- Base e > use Ada.Numerics.Generic_Elementary_Functions; > use Ada.Numerics; > ExpResult : Float; > begin > ExpResult := e ** Power; > return ExpResult; > end Recoded_Exp; Style notes: it's best not to use "use" unless it really cleans up the code (my rule is "unless the name is used at least three times"), and not to define a local variable unless it is really needed. > I've seen something that suggests I have to instantiate the generic > package, but I don't understand how. This is how: with Ada.Numerics.Generic_Elementary_Functions; function Recoded_Exp(Power: in Float) return Float is -- Base e package Float_Elementary is new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Float); use Float_Elementary; begin return Ada.Numerics.e ** Power; end Recoded_Exp; "Instantiating a generic package" means declaring a package using a generic template instead of providing all the code directly. So this line: package Float_Elementary is new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Float); declares the package "Float_Elementary", using the code from Generic_Elementary_Functions, with "Float_Type" replaced by "Float". -- -- Stephe