From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Generics crash course Date: Fri, 16 Jan 2015 07:43:33 +0200 Organization: Tidorum Ltd Message-ID: References: <121014ba-309c-45a0-950a-20b5b15210c5@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net nGmBuKldeH8ufOOZuEC1uAymgJRKhdXrHf+F2Bv1zIxYuoGh4Y Cancel-Lock: sha1:mfUy6pZRHm3e/9GMwm6Prb2BdX8= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: <121014ba-309c-45a0-950a-20b5b15210c5@googlegroups.com> Xref: news.eternal-september.org comp.lang.ada:24591 Date: 2015-01-16T07:43:33+02:00 List-Id: 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 . @ .