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 X-Received: by 2002:a24:58b:: with SMTP id 133-v6mr3525717itl.52.1527431686102; Sun, 27 May 2018 07:34:46 -0700 (PDT) X-Received: by 2002:a9d:4795:: with SMTP id b21-v6mr845000otf.10.1527431685834; Sun, 27 May 2018 07:34:45 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!85.12.16.69.MISMATCH!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.am4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!v8-v6no4276323itc.0!news-out.google.com!f20-v6ni4067itd.0!nntp.google.com!u74-v6no4249015itb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 27 May 2018 07:34:45 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=73.205.150.94; posting-account=Ru7E4QoAAAC_HiQ2D8LjZ7rh1mbTNcVn NNTP-Posting-Host: 73.205.150.94 References: <55ce14eb-6b83-4ea0-a550-f9e1410d0b06@googlegroups.com> <704ab1a8-c954-460e-81e7-05c64fcfaba4@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <71501ba6-766e-45df-927b-0ed908fba28e@googlegroups.com> Subject: Re: Strings with discriminated records From: NiGHTS Injection-Date: Sun, 27 May 2018 14:34:46 +0000 Content-Type: text/plain; charset="UTF-8" X-Received-Bytes: 2974 X-Received-Body-CRC: 3215285996 Xref: reader02.eternal-september.org comp.lang.ada:52726 Date: 2018-05-27T07:34:45-07:00 List-Id: 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");