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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a6b:bec1:: with SMTP id o184-v6mr4887108iof.8.1527435177705; Sun, 27 May 2018 08:32:57 -0700 (PDT) X-Received: by 2002:a9d:740a:: with SMTP id n10-v6mr315675otk.8.1527435177487; Sun, 27 May 2018 08:32:57 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!v8-v6no4319306itc.0!news-out.google.com!b185-v6ni3686itb.0!nntp.google.com!v8-v6no4319300itc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 27 May 2018 08:32:57 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=80.139.101.198; posting-account=rmHyLAoAAADSQmMWJF0a_815Fdd96RDf NNTP-Posting-Host: 80.139.101.198 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: AdaMagica Injection-Date: Sun, 27 May 2018 15:32:57 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:52730 Date: 2018-05-27T08:32:57-07:00 List-Id: Am Sonntag, 27. Mai 2018 17:19:37 UTC+2 schrieb NiGHTS: > 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 discr= iminant. 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 = access type > > >> > > >> There is no constructors and no user-defined aggregates, which you a= re > > >> actually asking for, but sometimes a constructing function does the = job: > > >> > > >> 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 = often but not in the context of strings, so I find this an elegant way to s= olve 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 simpl= y=20 > > write: > >=20 > > M : Message :=3D "Hello World"; > >=20 > > --=20 > > Regards, > > Dmitry A. Kazakov > > http://www.dmitry-kazakov.de >=20 > So I tried use your solution in another program I am writing but the comp= iler is complaining because the record I am doing this on is derived from A= da.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. >=20 > This gives me an error... >=20 > Type Message (Length : Positive) is new Ada.Finalization.Controlled with = record > Text : String (1 .. Length); > end record; > =20 =20 function Create (Value : String) return Message is begin return (Ada.Finalization.Controlled with Length =3D> Value'Length, Text =3D> Value); end Create;=20