comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Help with designing data structure for novice Ada programmer
Date: Sun, 9 Jan 2022 11:10:55 +0100	[thread overview]
Message-ID: <srecbf$1rpv$1@gioia.aioe.org> (raw)
In-Reply-To: sreb3f$g0e$1@dont-email.me

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

  reply	other threads:[~2022-01-09 10:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-08 20:41 Help with designing data structure for novice Ada programmer Aleksy Grabowski
2022-01-09  2:36 ` Paul Rubin
2022-01-09  9:49   ` Aleksy Grabowski
2022-01-09 10:10     ` Dmitry A. Kazakov [this message]
2022-01-14  5:01   ` 1.AAC0832
2022-01-14 16:05     ` Dennis Lee Bieber
2022-01-14 17:40       ` Dmitry A. Kazakov
replies disabled

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