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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c3a7c1845ec5caf9 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Equality operator overloading in ADA 83 Date: 1997/04/25 Message-ID: #1/1 X-Deja-AN: 237220032 References: <01bc4e9b$ac0e7fa0$72041dc2@lightning> <335CAEFE.35DC@elca-matrix.ch> <335E0A26.16D0@elca-matrix.ch> <33692089.5794807@news.airmail.net> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-04-25T00:00:00+00:00 List-Id: In article <33692089.5794807@news.airmail.net>, clines@delete_this.airmail.net (Kevin Cline) wrote: >>I mean, to append a single character to this array, I just want to >>increment the discrim, and stick the character in the array. But Ada >>rules require me to do an assignment on the whole thing. > >Another outstanding reason why Ada never became popular for desktop >applications. String manipulation with the Ada standard string types is a >major pain in the butt, and amazingly inefficient. This is a specious argument. The design of Ada's String type was chosen precisely because it was *more* efficient than the heap-based approach in other languages. I will tell you why programmers think Ada's string manipulation is a major pain. Many programmers don't know how to store the result of a function that returns an unconstrained string in Ada 83. Given this: function F (...) return String; Programmers innocently tried to do this: declare S : String := F (...); begin Incredibly, that's illegal in Ada 83. All that was required, though, was a simple repair: declare S : constant String := F (...); begin And it's incredible that many Ada programmers didn't know this. Many believe that the value of a constant object must be determined at compile time, so never declare constant objects. They "solved" their problem by never returning unconstrained strings from functions (some shops actually ban functions like that, making vague claims about efficiency), instead opting to always return a constrained, string buffer type (package CMN_String is...). Of course, that has the attendant problem of not being able to override the equality operator, so they either solve that by an Is_Equal method (that no one remembers to call) or making the string buffer limited (now _that's_ a real pain). Another idiom that few Ada programmer's know about is how to declare a mutable string object, or a ragged string array, or a queue of strings: declare S : ; begin S := "hello"; S := "goodbye"; or declare SA : := ("hello", "goodbye"); or declare The_Stack : ; begin Push ("hello", On => The_Stack); Push ("goodbye", On => The_Stack); Many attempt to "solve" this problem by manipulating string pointers, and putting varying length string objects on the heap. This is a perennial source of memory leaks, and manual reclamation of memory _is_ a pain (by design). But no heap is required: subtype Length_Range is Natural range 0 .. 132; type String_Buffer (Length : Length_Range := 0) is record Buffer : String (1 .. Length); end record; function "+" (Right : String) return String_Buffer is begin return (Right'Length, Right); end; O : String_Buffer_Array := (+"hello", +"goodbye"); Of course, there is a small problem with the String_Buffer declaration. Given this: procedure P (S : String) is The_Buffer : String_Buffer := +S; begin then declare O : String (2 .. 10); begin P (O); would raise Constraint_Error! Reason: the indices of object S didn't match the index constraints of type String_Buffer. But that's easy enough to solve: function "+" (Right : String) return String_Buffer is subtype Slided is String (1 .. Right'Length); begin return (Right'Length, Slided (Right)); end; procedure P (S : String) is The_Buffer : String_Buffer := +S; begin and then all is well. I could go on about concatenation of constrained strings also raising Constraint_Error (index constraints again), but I won't belabor the point. The other issue is that there wasn't a standard string manipulation library to perform white-space removal, left or right justification, that sort of thing. But all of these very real problems were cleaned up in Ada 95. So you are correct that string manipulation - in Ada 83 - can be a pain, but this is NOT the case for Ada 95, nor are "Ada strings" somehow more inefficient. Matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271