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-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!yWEAdf48JK7zWb9n7RvSRA.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Help with designing data structure for novice Ada programmer Date: Sun, 9 Jan 2022 11:10:55 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <87sftx1vpd.fsf@nightsong.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gioia.aioe.org; logging-data="61247"; posting-host="yWEAdf48JK7zWb9n7RvSRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:63360 List-Id: On 2022-01-09 10:49, Aleksy Grabowski wrote: > I also have some concrete question, how to properly implement optional > element? Right now I have something like this: > >     generic >         type T is private; >     package TypeUtils is >         type Optional(exists : Boolean) is >         record >             case exists is >                 when True => value : T; >                 when False => null; >             end case; >         end record; >     end TypeUtils; > > But it looks like it doesn't work, because it depends on the > discriminant `exists' but it is set once in .ads file and I can't modify > it in runtime. Provide a default value for the discriminant: type Optional (Defined : Boolean := False) is record case Present is when True => Value : T; when False => null; end case; end record; Now it is a definite type and you can place it into another record type: type Container is record ... Optional_Field : Optional; ... end record; and you can always update it: X : Container; X.Optional_Field := (True, Value_1); ... X.Optional_Field := (Defined => False); ... X.Optional_Field := (True, Value_2); ... -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de