From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Generics crash course
Date: Fri, 16 Jan 2015 07:43:33 +0200
Date: 2015-01-16T07:43:33+02:00 [thread overview]
Message-ID: <chrmo5Fdn12U1@mid.individual.net> (raw)
In-Reply-To: <121014ba-309c-45a0-950a-20b5b15210c5@googlegroups.com>
On 15-01-16 05:44 , John Smith wrote:
> Hello,
>
> I'm going through the wikibook about Ada. It has this example when
> it comes to generics:
> http://wikibook-ada.sourceforge.net/html/Algorithms__binary_search__adb.htm
>
> 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?
Line 12:
generic
says that the procedure Search is generic in that it depends on (uses)
some "generic formal parameters" which are listed and described on lines
13..21. In effect, Search is a "template" for a set of procedures which
differ according to how these formal generic parameters are filled in
with actual types and operations. Compare to lines 73..76 where an
"instance" of Search called Float_Search is created, with actual types
and operaions associated to the formals.
It is not possible to call Search, because it is generic; it is possible
to call Float_Search, which is a normal procedure (not generic anymore).
To "call" a generic procedure needs two steps: first instantiate (lines
73..76), then call (line 120). Of course, Float_Search can then be
called as many times as one likes. It is also possible to instantiate
Search several times, but each instance will be a different
(non-generic) procedure.
Line 13:
type Element_Type is private;
says that Search depends on a type with the formal name Element_Type,
but that Search assumes very little about the nature of the type,
because it is defined as "private". The actual type could be an integer,
a float, a record, an array (of fixed size), ...
Line 14:
type Index_Type is range <>;
says that Search also depends on a type with the formal name Index_Type,
and that Search assumes that this type is a "discrete" type such as an
integer type or an enumeration type.
Lines 15..16:
type Array_Type is
array (Index_Type range <>) of Element_Type;
say that Search depends on a type with the formal name Array_Type and
with the property that the index is of Index_Type (but the index range
is not fixed) and that the array contains elements of Element_Type.
Lines 17..21:
with function "<"
(Left : in Element_Type;
Right : in Element_Type)
return Boolean
is <>;
say that Search depends on a function with the formal name "<", with two
(normal, not generic) parameters of type Element_Type, and which returns
a Boolean. Line 21, "is <>", says that this formal generic parameter has
a default association with an actual function: if Search is instantiated
at a point where a function named "<" with the required parameter and
result profile is directly visible, then the instantiation text does not
have to associate an actual function with the formal "<", and the
suitable actual "<" function is automatically used. This is why there is
no association for the formal "<" in lines 73..76: thanks to the "is <>"
there is an implicit association
"<" => "<",
which tells the compiler to use whatever visible "<" function has the
right profile, and of course this is the (predefined) "<" for Float.
Note that this has nothing directly to do with overloading. The formal
generic function could be named Xjoids; if it has the default "is <>"
and the instance does not associate it with an actual function, then the
compiler would look for an actual function called Xjoids, with the right
profile, whether or not there are overloaded forms of Xjoids.
> 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, "exit" in Ada means to exit (break) from the (innermost) containing
loop, not to stop the program. So in this example, the "exit" on line 53
means that execution continues on line 67 (which means that Search
returns to its caller). This "exit" statement could therefore be
replaced by a "return" statement (but using "exit" here is better style
IMO).
To stop the program (without calling some O/S function not defined in
the Ada standard), one must cause the main procedure to return, or
terminate in some way. One way is to raise an exception that propagates
to the main procedure.
By the way, all the "return" statements in the example (lines 68, 142,
155) are superfluous, because a subprogram returns when its "end" is
reached.
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
next prev parent reply other threads:[~2015-01-16 5:43 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
2015-01-16 5:33 ` Dirk Heinrichs
2015-01-16 5:43 ` Niklas Holsti [this message]
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