comp.lang.ada
 help / color / mirror / Atom feed
From: NiGHTS <nights@unku.us>
Subject: Re: Strings with discriminated records
Date: Sun, 27 May 2018 08:19:36 -0700 (PDT)
Date: 2018-05-27T08:19:36-07:00	[thread overview]
Message-ID: <b5891005-8667-4ada-a75e-9565f631954f@googlegroups.com> (raw)
In-Reply-To: <peegke$rt1$1@gioia.aioe.org>

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 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

So I tried use your solution in another program I am writing but the compiler 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 record
    Text  :  String (1 .. Length);
end record;
       
function Create (Value : String) return Message is
    Finalization : Ada.Finalization.Controlled; -- "ERROR: type of object cannot be abstract"
begin
    return (Finalization with Length => Value'Length, Text => Value);
end Create; 


Hmm, any ideas?

  reply	other threads:[~2018-05-27 15:19 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-26 21:43 Strings with discriminated records NiGHTS
2018-05-26 23:42 ` Shark8
2018-05-27  1:42   ` NiGHTS
2018-05-27  8:39     ` Dmitry A. Kazakov
2018-05-27 12:22       ` Mehdi Saada
2018-05-27 12:40         ` Dmitry A. Kazakov
2018-05-27 14:34       ` NiGHTS
2018-05-27 14:50         ` Dmitry A. Kazakov
2018-05-27 15:19           ` NiGHTS [this message]
2018-05-27 15:32             ` AdaMagica
2018-05-27 16:22               ` NiGHTS
2018-05-29 22:31           ` Randy Brukardt
2018-05-30  7:29             ` Dmitry A. Kazakov
2018-05-30 20:11               ` Randy Brukardt
2018-05-27 12:48     ` Mehdi Saada
2018-05-27 13:03       ` Dmitry A. Kazakov
2018-05-27 17:11 ` NiGHTS
2018-05-27 18:07   ` Simon Wright
2018-05-27 23:08     ` NiGHTS
2018-05-28  1:44       ` Jere
2018-05-28  3:05         ` NiGHTS
2018-05-28  3:23           ` NiGHTS
2018-05-27 18:25   ` Dmitry A. Kazakov
2018-05-27 22:44     ` NiGHTS
2018-05-28  7:29       ` Dmitry A. Kazakov
2018-05-28  7:42       ` Simon Wright
2018-05-28 18:38         ` Shark8
2018-05-28 21:15           ` Mehdi Saada
2018-05-28 21:48             ` Shark8
2018-05-28 22:27               ` Mehdi Saada
2018-05-28 23:59                 ` Shark8
2018-05-29  0:41                   ` Dan'l Miller
2018-05-30 17:11                     ` Shark8
2018-05-29  7:49                 ` Dmitry A. Kazakov
2018-05-29  9:31                   ` AdaMagica
2018-05-29 10:14                     ` Dmitry A. Kazakov
2018-05-29 13:40                   ` Dan'l Miller
2018-05-29 14:04                     ` Dmitry A. Kazakov
2018-05-29 22:41                       ` Randy Brukardt
2018-05-30  5:00                         ` J-P. Rosen
2018-05-30 20:09                           ` Randy Brukardt
2018-05-31  4:19                             ` J-P. Rosen
2018-05-31 22:18                               ` Randy Brukardt
2018-06-01 13:35                                 ` Dan'l Miller
2018-06-01 15:20                                   ` Dmitry A. Kazakov
2018-05-28 13:55 ` NiGHTS
2018-05-29 14:37 ` Mehdi Saada
2018-05-29 22:44   ` Randy Brukardt
2018-05-29 22:41 ` Mehdi Saada
2018-05-30 19:46   ` Randy Brukardt
2018-05-30 19:48     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox