comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Understanding generic package functions
Date: Tue, 03 Nov 2015 03:40:32 -0600
Date: 2015-11-03T03:40:32-06:00	[thread overview]
Message-ID: <86io5j2zfz.fsf@stephe-leake.org> (raw)
In-Reply-To: cb181260-a5d3-4b5b-9a40-c925f7100b93@googlegroups.com

Nick Gordon <annihilan@gmail.com> 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

      parent reply	other threads:[~2015-11-03  9:40 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-03  0:45 Understanding generic package functions Nick Gordon
2015-11-03  3:36 ` Jeffrey R. Carter
2015-11-03  6:59 ` Randy Brukardt
2015-11-03  7:59   ` Nick Gordon
2015-11-03  9:15     ` briot.emmanuel
2015-11-03 17:27     ` Jeffrey R. Carter
2015-11-03  9:40 ` Stephen Leake [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