comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: Initializing an array of tasks with discrimants
Date: Sat, 23 Nov 2019 08:57:20 -0800 (PST)
Date: 2019-11-23T08:57:20-08:00	[thread overview]
Message-ID: <371cb59c-1814-4953-9652-43c9986808a1@googlegroups.com> (raw)
In-Reply-To: <358a3934-7cb2-4cff-8703-71410815f27e@googlegroups.com>

On Friday, November 22, 2019 at 5:07:49 PM UTC-5, Jere wrote:
> I recently ran into a situation where I had an array
> of tasks that had discriminants (with a default), but 
> I couldn't find a simple way to initialize them.  I 
> did come up with a workaround, but wanted to see if
> the language defined a way to do this:
> 
>     task type T(D: Integer := 3);
>     
>     task body T is 
>     begin 
>         null; 
>     end T;
>     
>     type A is array (Integer range <>) of T;
>     
>     V : A(1..10) := ?????????
> 
> If they all had the same discriminant value, I
> can just create a subtype and do an array of 
> the subtype, but in this case, the discriminants
> would not all be the same.
> 
> My work around was a wrapper type:
> 
>     type Wrapper(D : Integer := 0) is record
>         Impl : T(D);
>     end record.
> 
>     type A is array (Integer range <>) of Wrapper;
> 
>     V : A(1..10) := (1 => (D => 1, others => <>), and so on
> 
> But I was hoping there was a better way to do it simply.
> The Wrapper record isn't there to represent the 
> intended design and doesn't really improve readability.
> It's essentially a workaround for something I cannot 
> figure out.
> 
> I tried many variations of qualified expressions and 
> aggregates on the original task type elements, but all
> of them gave me errors about it expected the task type
> but found a composite type instead.
> 
> Is there a particular aggregate that I can use to avoid
> the superfluous wrapper record?

One other option I didn't think of which is definitely 
better than the wrapper record is a constructing function:

   function Make_Task(Option : Integer) return T is
   begin
      return Result : Task(Option);
   end Make_Task;

That lets me do things like:

   A : T_Array(1..10) :=
      (1 => Make_Task(2),
       2 => Make_Task(5),
       others => Make_Task(20));

I still think Ada should support aggregates for Tasks
with discriminants in some form.  I'm hoping the 2020 
change mentioned earlier will help with that (though I
never identified how when reading over it...I'll spend
some more time looking for it).

  parent reply	other threads:[~2019-11-23 16:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-22 22:07 Initializing an array of tasks with discrimants Jere
2019-11-22 22:39 ` Jeffrey R. Carter
2019-11-23 16:23   ` Jere
2019-11-22 23:57 ` Robert A Duff
2019-11-23  0:27   ` Anh Vo
2019-11-23  9:12   ` Randy Brukardt
2019-11-23 16:50     ` Jere
2019-11-23 16:25   ` Jere
2019-11-23  8:29 ` Dmitry A. Kazakov
2019-11-23 16:28   ` Jere
2019-11-23 17:03     ` Dmitry A. Kazakov
2019-11-23 16:57 ` Jere [this message]
2019-11-25 22:36   ` 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