comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Generics crash course
Date: Thu, 15 Jan 2015 22:14:09 -0700
Date: 2015-01-15T22:14:09-07:00	[thread overview]
Message-ID: <m9a6m5$hr0$1@dont-email.me> (raw)
In-Reply-To: <121014ba-309c-45a0-950a-20b5b15210c5@googlegroups.com>

On 01/15/2015 08:44 PM, John Smith wrote:
> 
> What I don't understand is from lines 12 to 21.  How does this work exactly?
> I mean, does this somehow "connect" the overloaded < operator to the Search
> procedure?

You can think of a generic as a kind of template for producing actual packages
and subprograms (the example here has a generic procedure). Lines 12-21 are the
"generic formal part" that specifies the "generic formal parameters". These tell
the user what needs to be provided to use the generic. The rest of the generic
is written in terms of these parameters. The instantiation (line 73) creates an
actual procedure (in this case) from the generic that acts upon the actual
parameters in the instantiation in place of the formal parameters.

This is not a great example, since the generic is just a complication in
implementing a binary search of an array of Float indexed by Positive. A better
example might have shown searching multiple arrays of different element types,
indexed by different index types.

As an example, Search could be instantiated for

type Person is record
   ID   : Positive;
   Name : Ada.Strings.Unbounded.Unbounded_String;
   DOB  : Ada.Calendar.Time;
end record;

function "<" (Left : in Person; Right : in Person) return Boolean is
begin -- "<"
   return Left.ID < Right.ID;
end "<";

subtype PLI is Long_Integer range 1 .. Long_Integer'Last;

type Person_List is array (PLI range <>) of Person;

procedure Find is new Search
(Element_Type => Person, Index_Type => PLI, Array_Type => Person_List);

and used to find people by ID.

> Also, on line 53, if this code is ever reached, then this means that the
> entire application will stop executing, yes?  The procedure won't just return
> to the caller.

No. Line 53 is an exit statement. A plain exit statement like this
unconditionally exits the innermost enclosing loop statement. An "exit" with a
loop name can exit any enclosing loop, not just the innermost. An "exit" with a
"when" part is a conditional "exit" and only exits when its condition is true.

exit [loop-name] [when condition];

I would recommend you learn more about basic Ada (such as loop and exit
statements) before attacking more advanced topics such as generics.

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33


  reply	other threads:[~2015-01-16  5:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-16  3:44 Generics crash course John Smith
2015-01-16  5:14 ` Jeffrey Carter [this message]
2015-01-16  5:33 ` Dirk Heinrichs
2015-01-16  5:43 ` Niklas Holsti
2015-01-16  6:27   ` Jeffrey Carter
replies disabled

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