comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Understanding generic package functions
Date: Mon, 2 Nov 2015 20:36:41 -0700
Date: 2015-11-02T20:36:41-07:00	[thread overview]
Message-ID: <n19a04$m7m$1@dont-email.me> (raw)
In-Reply-To: <cb181260-a5d3-4b5b-9a40-c925f7100b93@googlegroups.com>

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


  reply	other threads:[~2015-11-03  3:36 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 [this message]
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
replies disabled

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