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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!stamper.news.atl.earthlink.net!newsread2.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) References: <41547dae$0$91007$39cecf19@news.twtelecom.net> <41583b4c$0$74190$39cecf19@news.twtelecom.net> From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 29 Sep 2004 13:44:08 GMT NNTP-Posting-Host: 64.185.133.124 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.atl.earthlink.net 1096465448 64.185.133.124 (Wed, 29 Sep 2004 06:44:08 PDT) NNTP-Posting-Date: Wed, 29 Sep 2004 06:44:08 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:4382 Date: 2004-09-29T13:44:08+00:00 List-Id: kevin.cline@gmail.com (Kevin Cline) writes: > Matthew Heaney wrote in message > news:... > > > > You're still confused. The local subprograms are analogous to STL > > binders and function objects. The generic part is Generic_Op. The > > rest is just glue. > > But 14 lines of glue for every different array type that I want to use > with Generic_Op? That's the sort of repetition that turned me away > from Ada in the first place. I like languages where I can say what I > want, and only have to say it once. I don't like languages where I > have to cut and paste endless variations of boilerplate code. For > that reason I like C++ and Perl and LISP and don't like Ada and Java > and C#. I should have pointed out that there's nothing that precludes this in Ada. It's exactly analogous to template specializations in C++. I gave an example of a Generic_Op that works with a container interface, and then made that work with an array. But of course, we could have hidden this glue code behind a specialization of the algorithm for array types: generic type Cursor is private; ... procedure Generic_Op (...); generic type IT is (<>); type ET is private; type Array_T is array (IT) of ET; ... procedure Generic_Op_Constrained_Array (...); generic type IT is (<>); type ET is private; type Array_T is array (IT range <>) of ET; ... procedure Generic_Op_Unconstrained_Array (...); generic type IT is (<>); with procedure Swap (I, J : IT) is <>; ... procedure Generic_Op_Anonymous_Array (...); generic type IT is (<>); type ET (<>) is limited private; type Array_T is array (IT range <>) of access ET; ... procedure Generic_Op_Array_Access_Component (...); You get the idea. This is no different from the C++ technique of specializing a generic algorithm on the type. I said in my previous post that the local subprograms often make up-level references to the enclosing scope. Another example of that is if I declare an anonymous array object: declare A : array (1 .. N) of access Shape'Class; begin Pretend Generic_Op is a sort algorithm. In order to sort this array object, I need to use the most general specialization for arrays (it's the one listed above that accepts a Swap procedure), because I don't have an array type to use as the generic actual. Something like: declare A : array (1 .. N) of access Shape'Class; begin ... Sort_A: declare function Less (I, J : Positive) return Boolean is begin return A (I) < A (J); --or whatever end; procedure Swap (I, J : Positive) is SI : Shape'Class renames A (I).all'Access; begin A (I) := A (J); A (J) := SI; end; procedure Sort is new Generic_Sort_Anonymous_Array (Positive); begin Sort (A'First, A'Last); end Sort_A; ... end; Here the local subprograms provide the glue to bind a pure algorithm to an actual object. The generic is named Generic_Sort_Anonymous_Array, but really that algorithm works for any array-like thing that uses a discrete index to refer to elements. You could use it to sort a vector or deque, for example. This example was motivated by the ai302/examples/shapes example, which was inspired by the example in Musser (who was himself inspired by the same example in Strustroup). -Matt