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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no 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!postnews1.google.com!not-for-mail From: kevin.cline@gmail.com (Kevin Cline) Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: 27 Sep 2004 19:47:28 -0700 Organization: http://groups.google.com Message-ID: References: <41547dae$0$91007$39cecf19@news.twtelecom.net> <41583b4c$0$74190$39cecf19@news.twtelecom.net> NNTP-Posting-Host: 24.1.141.253 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1096339648 24277 127.0.0.1 (28 Sep 2004 02:47:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 28 Sep 2004 02:47:28 +0000 (UTC) Xref: g2news1.google.com comp.lang.ada:4297 Date: 2004-09-27T19:47:28-07:00 List-Id: "Matthew Heaney" wrote in message news:<41583b4c$0$74190$39cecf19@news.twtelecom.net>... > "Kevin Cline" wrote in message > news:e749549b.0409270631.2bf81ccf@posting.google.com... > > Matthew Heaney wrote in message > news:... > > > kevin.cline@gmail.com (Kevin Cline) writes: > > > > > > Indeed, you cannot do this. In AI-302 (and in Ada.Strings.*), you have > to say: > > > > > > Replace_Element (C, Index => 5, By => 7); > > > > I think this was a major error in the language design. As a result it > > very tedious to write a generic that will work with both predefined > > and user-defined types. > > All you need to do is write the generic in terms of (AI-302) container > operations, and then use locally declared subprograms to make the array > (say) work with the generic algorithm. The "tedium" only applies when using > a generic on an array instead of a container. > > For example: > > generic > type Cursor is private; > with function Next (C : Cursor) return Cursor is <>; > with procedure Replace_Element (C : Cursor; By : ET) is <>; > with function Has_Element (C : Cursor) return Boolean is <>; > procedure Generic_Op (C : Cursor); > > To use this with a container type, there's nothing you need to do except > instantiate the generic algorithm. > > To make this work with an array, all you need to do is: > > procedure Op (A : in out Array_Type) is > > procedure Replace_Element (I : Index_Type; E : ET) is > begin > A (I) := E; > end; > > function Has_Element (I : IT) return Boolean is > begin > return I in A'Range; > end; > > procedure Algorithm is > new Generic_Algorithm (Index_Type, Index_Type'Succ); > > begin > Algorithm (A'First); > end Op; > > Note that here the "cursor" is just the discrete array index subtype. The > T'Succ attribute is used as the generic actual for the Next generic formal. > > Your problem is that you're still thinking in C++ terms. No doubt. > C++ works by > making containers look like built-in types. That's different from Ada, > which (as demonstrated above) makes the built-in type look like a container. Ok, but shouldn't that code be generic? The code above works for only one algorithm on one particular array type. I think that "different from Ada" here is significantly better. Lots of existing C and C++ code that works on arrays that can be trivially modified to work on any array-like class, but existing Ada array code has to be rewritten in every detail to work on a user-defined container. And the results of that rewrite aren't pretty. For example, suppose you want to multiply sparse matrices. Instead of the natural: P(I,K) := A(I,J) * B(J,K) you'll end up with something like: SET_ELEMENT(P, I, K, ELEMENT(A, I, J) * ELEMENT(B, J, K))