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:a6b:df08:: with SMTP id w8-v6mr1690239iog.32.1527434376900; Sun, 27 May 2018 08:19:36 -0700 (PDT) X-Received: by 2002:a9d:4503:: with SMTP id w3-v6mr851066ote.7.1527434376632; Sun, 27 May 2018 08:19:36 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!feeder4.usenet.farm!feed.usenet.farm!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!85.12.16.68.MISMATCH!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.am4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!v8-v6no4309710itc.0!news-out.google.com!b185-v6ni3686itb.0!nntp.google.com!v8-v6no4309709itc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 27 May 2018 08:19:36 -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> <71501ba6-766e-45df-927b-0ed908fba28e@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Strings with discriminated records From: NiGHTS Injection-Date: Sun, 27 May 2018 15:19:36 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 4669 X-Received-Body-CRC: 1706065182 Xref: reader02.eternal-september.org comp.lang.ada:52729 Date: 2018-05-27T08:19:36-07:00 List-Id: On Sunday, May 27, 2018 at 10:50:58 AM UTC-4, Dmitry A. Kazakov wrote: > 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 discrim= inant. Using your example, something like this... > >>> > >>> Type Message (Length : Positive; Text_Value : String) is record > >>> Text : String(1..Length) :=3D Text_Value; > >>> -- Other components. > >>> end record; > >>> > >>> This gives me an error: Discriminants must must have a discrete or ac= cess type > >> > >> There is no constructors and no user-defined aggregates, which you are > >> actually asking for, but sometimes a constructing function does the jo= b: > >> > >> function Create (Value : String) return Message is > >> begin > >> return (Length =3D> Value'Length, Text =3D> Value); > >> end Create; > >> > >> --=20 > >> Regards, > >> Dmitry A. Kazakov > >> http://www.dmitry-kazakov.de > >=20 > > This is actually a good solution to me. I use this kind of technique of= ten but not in the context of strings, so I find this an elegant way to sol= ve my problem. Here is the final code for anyone who want to see it. > >=20 > > Type Message (Length : Positive) is record > > Text : String (1 .. Length); > > end record; > > =20 > > function Create (Value : String) return Message is > > begin > > return (Length =3D> Value'Length, Text =3D> Value); > > end Create; > > =20 > > M : Message :=3D Create ("Hello World"); >=20 > Some reduce it to an operator >=20 > function "+" (Value : String) return Message is > begin > return (Length =3D> Value'Length, Text =3D> Value); > end "+"; >=20 > M : Message :=3D +"Hello World"; >=20 > as a poor-man solution to supertyping. I.e. if it were possible to make= =20 > Message a subtype of String (by providing conversions), you could simply= =20 > write: >=20 > M : Message :=3D "Hello World"; >=20 > --=20 > Regards, > Dmitry A. Kazakov > http://www.dmitry-kazakov.de So I tried use your solution in another program I am writing but the compil= er is complaining because the record I am doing this on is derived from Ada= .Finalization.Controlled, so I'd need to use an extension aggregate, but I'= m not sure how to do that with an abstract object like this. This gives me an error... Type Message (Length : Positive) is new Ada.Finalization.Controlled with re= cord Text : String (1 .. Length); end record; =20 function Create (Value : String) return Message is Finalization : Ada.Finalization.Controlled; -- "ERROR: type of object c= annot be abstract" begin return (Finalization with Length =3D> Value'Length, Text =3D> Value); end Create;=20 Hmm, any ideas?