From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!feeder1.feed.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Arrays with discriminated task components Date: Sun, 25 Dec 2022 18:16:56 +0200 Organization: Tidorum Ltd Message-ID: References: <9b729cf0-02c7-48e1-8cea-c0d177c4bf3bn@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net SebDP3aXLB760DJUQmROgAjfvQE5A89eyEN5D8WezMUzgkQ5/H Cancel-Lock: sha1:98EQCZ1P2G8wOwSUCgK40GkLX7g= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Content-Language: en-US In-Reply-To: <9b729cf0-02c7-48e1-8cea-c0d177c4bf3bn@googlegroups.com> Xref: reader01.eternal-september.org comp.lang.ada:64732 List-Id: On 2022-12-24 13:44, AdaMagica wrote: > I've got a task type with a discriminant: > > type Index is range 1 .. N; > > task type T (D: Index); > > Now I want an array of these tasks, where each task knows its > identity (the index) via the discriminant, an iterated_component_association: > > Arr: array (Index) of T := (for I in Index => ???); > > How can I do this? > > This works with access, but I find this extremely ugly: > > Arr: array (Index) of access T := (for I in Index => new T (I)); > > Alternatively, I could use the traditional method with a Start entry with the index as parameter: > > task type T is > entry Start (D: Index); > end T; This seems to work with gnat, but I'm not entirely sure if it is legal (could there be a conflict between the default value of the task discriminant, which is the same for all tasks in the array, and the actual discriminants which are different for each task in the array?): N : constant := 10; type Index is range 1 .. N; task type T (D: Index := Index'First); -- A default value for D is needed to make the type constrained, as -- required by the Arr declaration below. function New_T (I : in Index) return T is begin return TI : T (D => I) do null; end return; end New_T; Arr: array (Index) of T := (for I in Index => New_T(I)); Whether this is any less ugly than the heap allocation method is doubtful.