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!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Strings with discriminated records Date: Sun, 27 May 2018 16:50:55 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <55ce14eb-6b83-4ea0-a550-f9e1410d0b06@googlegroups.com> <704ab1a8-c954-460e-81e7-05c64fcfaba4@googlegroups.com> <71501ba6-766e-45df-927b-0ed908fba28e@googlegroups.com> NNTP-Posting-Host: CvkHMVp693S8Z+lk11jyqg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:52727 Date: 2018-05-27T16:50:55+02:00 List-Id: On 2018-05-27 16:34, NiGHTS wrote: > On Sunday, May 27, 2018 at 4:39:36 AM UTC-4, Dmitry A. Kazakov wrote: >> On 2018-05-27 03:42, NiGHTS wrote: >>> On Saturday, May 26, 2018 at 7:42:22 PM UTC-4, Shark8 wrote: >>>> You can do this: >>>> >>>> Type Message(Length : Positive) is record >>>> Text : String(1..Length); >>>> -- Other components. >>>> end record; >>> >>> More interesting to me would be to pass a string literal as a discriminant. Using your example, something like this... >>> >>> Type Message (Length : Positive; Text_Value : String) is record >>> Text : String(1..Length) := Text_Value; >>> -- Other components. >>> end record; >>> >>> This gives me an error: Discriminants must must have a discrete or access type >> >> There is no constructors and no user-defined aggregates, which you are >> actually asking for, but sometimes a constructing function does the job: >> >> function Create (Value : String) return Message is >> begin >> return (Length => Value'Length, Text => Value); >> end Create; >> >> -- >> Regards, >> Dmitry A. Kazakov >> http://www.dmitry-kazakov.de > > This is actually a good solution to me. I use this kind of technique often but not in the context of strings, so I find this an elegant way to solve my problem. Here is the final code for anyone who want to see it. > > Type Message (Length : Positive) is record > Text : String (1 .. Length); > end record; > > function Create (Value : String) return Message is > begin > return (Length => Value'Length, Text => Value); > end Create; > > M : Message := Create ("Hello World"); Some reduce it to an operator function "+" (Value : String) return Message is begin return (Length => Value'Length, Text => Value); end "+"; M : Message := +"Hello World"; as a poor-man solution to supertyping. I.e. if it were possible to make Message a subtype of String (by providing conversions), you could simply write: M : Message := "Hello World"; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de