comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Variant record with multiple discriminants?
Date: Sat, 28 Oct 2017 21:47:33 +0300
Date: 2017-10-28T21:47:33+03:00	[thread overview]
Message-ID: <f5k1m5F3ab6U1@mid.individual.net> (raw)
In-Reply-To: <ot2dc5$osg$1@gioia.aioe.org>

On 17-10-28 20:05 , Victor Porton wrote:
> Ada does not support variant records whose fields presence depends on
> multiple discriminants, does it?

Yes it does, in two ways:

- a component in a variant (RM 3.8.1) can itself be of a discriminated 
record type, and can take the actual value of its discriminant from some 
discriminant of the containing record

- the component_list in a variant can contain nested variant_parts, that 
is, the "case .. when .. when .. end case" structure can be nested.

Example:

    type Int_Or_Char_T (Is_Int : Boolean)
    is record
       case Is_Int is
       when True  => I : Integer;
       when False => C : Character;
       end case;
    end record;

    type Float_Or_Int_Or_Char_1_T (Is_Float : Boolean; Is_Int : Boolean)
    is record
       case Is_Float is
       when True  => F : Float;
       when False => IC : Int_Or_Char_T (Is_Int);
       end case;
    end record;

    type Float_Or_Int_Or_Char_2_T (Is_Float : Boolean; Is_Int : Boolean)
    is record
       case Is_Float is
       when True  => F : Float;
       when False =>
          case Is_Int is
          when True  => I : Integer;
          when False => C : Character;
          end case;
       end case;
    end record;

The main limitation is that the values used in the "case" constructs, or 
supplied as actual discriminant values for sub-records, must be 
discriminants of the parent record, and cannot be computed expressions. 
For example, it would be illegal to declare the "IC" component in this 
way, using a Boolean expression as the discriminant value:

       when False => IC : Int_Or_Char_T (not Is_Int);

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


  reply	other threads:[~2017-10-28 18:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-28 17:05 Variant record with multiple discriminants? Victor Porton
2017-10-28 18:47 ` Niklas Holsti [this message]
2017-10-28 18:48 ` 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