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!.POSTED!not-for-mail From: Bob Duff Newsgroups: comp.lang.ada Subject: Re: Bounded String question Date: Wed, 11 Nov 2015 10:41:34 -0500 Organization: A noiseless patient Spider Message-ID: <87a8qk7dch.fsf@theworld.com> References: <7ba56b33-28d4-42d2-8b9b-5ad9f5beab8b@googlegroups.com> <87io597447.fsf@theworld.com> <66278720-249a-4191-a908-bb840e7f3ccc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b95a1959649799dee38fd87fab0df4e4"; logging-data="16534"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/VstSXHe4h9xe6v3nMJW5t" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:o8/jHeu/vuYR/JrRVUwTpMfs1w4= sha1:VBeeUYxv/uWUW9xG+Jnz15FHVUI= Xref: news.eternal-september.org comp.lang.ada:28302 Date: 2015-11-11T10:41:34-05:00 List-Id: Serge Robyns writes: > Hi Bob, > > On Wednesday, 11 November 2015 01:48:43 UTC+1, Bob Duff wrote: >> How about: >> >> function No_Client_Name return T_Client_Name is >> (T_Client_Name (P_Strings.Null_Bounded_String)); > > May idea was to have a "constant" and use language construct/semantic to expose > that. I was expecting that the compiler will have more optimization options > with such a construct than a dynamic construct like above. I doubt that. Especially if you put Inline. >> But why do you need Preelaborate? It's not all that much use if you use >> GNAT's static elaboration model. > > Just for the sake of "cleanness" and again using language features to express > my intent as per my "expectations". Even if I would end up using GNAT in all > cases. One possibility is to use the option in GNAT that tells you where to put pragma Elaborate_All, and put them in by hand. Then you don't need Preelaborate. >> > Maybe I'm using bounded strings completely wrong. >> >> Well, I think the Ada.Strings.Bounded package is way overengineered. >> So "using Ada.Strings.Bounded" = "using bounded strings wrong". ;-) >> I suggest rolling your own. No need for generics. >> >> type Bounded_String (Max_Length : Natural := ...) is limited record >> Length : Natural := 0; >> Chars : String (1 .. Max_Length); >> end record; >> >> along with a few trivial operations. > > I tend to agree that the bounded strings seems over-engineered but I'm lacking > Ada experience to judge that properly. However it is indeed a pain to use > compared to other languages where I come from, e.g. C. I don't know why > strings are so complex to use in Ada. At various places I've conversions > between bounded strings and plain strings in order to offer a consistent API to > my modules. Luckily the package already provide a To_String function. However, > I've had to write loads of functions like "To_Client_Name (Name : in String) > return T_Client_Name is (T_Client_Name (P_Strings.To_Bounded_String (Name));" > as I haven't found a more direct way to convert a string to the type. Can't you call the inherited To_Bounded_String directly? >...This with > some other oddities in Ada is what do give me a lot of frustrations. However, > I do remain decided to proceed with Ada, as I do believe in its core principles > of allowing developing reliable software. > > Now with regards to rolling my own, is this not defeating re-use? Why shall I > waste my time on such a feature while I'm having loads of things to do on the > application itself. I'm all for reuse. But in this case, the thing you're reusing causes a fair amount of trouble, and rolling your own using the type I showed above is trivial. > I've been considering using unbounded strings instead but then I'm dropping the > idea of a bounded storage for my entities. Most of these "strings" ends up in > record types which in the end will map to database entities. Right, if you don't have a known bound, and you're not developing for a small embedded system, then unbounded strings are often appropriate. Those are complicated enough that I would not recommend rolling your own. Another alternative is Ada.Containers.Vectors instantiated with Character. - Bob