comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Gordon <annihilan@gmail.com>
Subject: Re: Understanding generic package functions
Date: Mon, 2 Nov 2015 23:59:46 -0800 (PST)
Date: 2015-11-02T23:59:46-08:00	[thread overview]
Message-ID: <6a1eb518-801f-4a1d-af55-e122c2570b12@googlegroups.com> (raw)
In-Reply-To: <n19m0m$hgm$1@loke.gir.dk>

On Monday, November 2, 2015 at 9:36:45 PM UTC-6, Jeffrey R. Carter wrote:
> 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

Oh! I see! Suddenly this makes much more sense. I appreciate this a lot! I see that it wasn't the declaration of Ada.Numerics.e that was the issue, but the generic package. How though do I use the overridden binary operator ** in that case? Do I call X MyPack."**" Y or is there some way for me to inform the compiler do it, like use MyPack;?

On Tuesday, November 3, 2015 at 12:59:35 AM UTC-6, Randy Brukardt wrote:
> "Nick Gordon" <annihilan@gmail.com> wrote in message 
> news:cb181260-a5d3-4b5b-9a40-c925f7100b93@googlegroups.com...
> ...
> > 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.
> 
> Jeff gave you the long answer, which is better for most purposes. 
> Specifically for type Float (which you really should avoid itself, 
> preferring to use types of your own definition), Ada offers a 
> pre-instantiated version of the package called 
> "Ada.Numerics.Elementary_Functions", so you could use that instead.
> 
> > In general, I'm noticing that the learning curve for Ada is steep, and 
> > there are not
> > terribly many "quick references" for the language.
> 
> Ada Distilled is the best "quick reference", but there's really nothing 
> quick about using Ada. It's a professional level tool and thus it has lots 
> of things that you need to know. (Consider the difference between using a 
> home copy machine and a professional offset printing press.)
> 
>                                    Randy.

For better or worse, I've just ordered for $10 total two of the books listed in the adaic.org's "learning resources" section, so I'm hoping that they will be a better learning tool than Ada Distilled, which I already have. Again, thanks to the both of you.

Nick

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