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,c6aaff9993e444f0,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.212.232 with SMTP id nn8mr13953440pbc.1.1324211682049; Sun, 18 Dec 2011 04:34:42 -0800 (PST) Path: lh20ni36624pbb.0!nntp.google.com!news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Natasha Kerensikova Newsgroups: comp.lang.ada Subject: String_Holder ? Date: Sun, 18 Dec 2011 12:34:41 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Injection-Date: Sun, 18 Dec 2011 12:34:41 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="7fyd7lCvGVUn5eibBXudkw"; logging-data="18488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182PSK6n8HrwgtUcGiBxCDQ" User-Agent: slrn/0.9.9p1 (FreeBSD) Cancel-Lock: sha1:6rDijycH/80zm5oLwqCZJ3+aM+U= Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: 2011-12-18T12:34:41+00:00 List-Id: Hello, in my few Ada projects so far, I have quite often encountered the need of storing a string along with other information in records. So I went for discriminants containing the string size, and putting the string directly in the record. This works well when there is only one or two strings, but with several of them, or in variant records, I find it quite heavy. So I was thinking about something like that: type String_Holder is still to be defined; function Hold (S : String) return String_Holder; function To_String (Holder : String_Holder) return String; procedure Query (Holder : String_Holder; Process : not null access procedure (S : String)); I'm still unsure on what kind of type String_Holder should be (private, tagged private or interface). It would basically be a constant-size reference to an immutable string that is stored "somewhere". The simplest implementation would be an Unbounded_String, though if needed it could be improved reference counting to make assignments cheaper, or with a hash table to deduplicate identical strings, etc. So my question is, does it make sense to have such objects? Or am I getting blinded by my C past, where strings are always manipulated through a char* object? Assuming it does make sense, am I right in thinking it's better to have such a type, even if it's a thin wrapper around Unbounded_String, instead of using directly Unbounded_String? Assuming it does make sense to have a String_Holder, would it be better to have it private, tagged private or interface? My guess is that tagged private has no advantage over interface, especially with a default implementation around Unbounded_String, which doesn't need to be controlled, but then controlled extension would be heavier. Then the choice between private and interface amounts to whether or not it could be useful to use simultaneously several implementations of String_Holder in the same project. I cannot think of any project where it would be the case, so I would lean towards private, but maybe I'm lacking imagination here. Still under the same assumption, is the above specification sane, or am I missing something? Query procedure is probably not absolutely necessary, but I guess it can be occasionally useful to save a string copy compared to To_String, and it seems to be a very small implementation burden anyway. Still under the same assumption, can it be useful to provide unary "+" functions to tersely convert String_Holder to String, and maybe vice-versa, or would it do more harm (obfuscation) than good? And lastly, is there any trick to allow a String_Holder object or similar to be pre-elaborable? Thanks in advance for your insights, Natasha