comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: Disriminant question
Date: 7 Mar 2003 08:46:10 -0800
Date: 2003-03-07T16:46:11+00:00	[thread overview]
Message-ID: <1ec946d1.0303070846.252c6407@posting.google.com> (raw)
In-Reply-To: slrnb6gvth.nt.lutz@taranis.iks-jena.de

Lutz Donnerhacke <lutz@iks-jena.de> wrote in message news:<slrnb6gvth.nt.lutz@taranis.iks-jena.de>...
> * prashna wrote:
> > How to change discriminant value dynamically?
> 
> This is impossible. You can't change the discriminant, because the
> instantiated variables does occupy different space. But you can set the
> discriminant dynamically.

The rule is that you can't change the discriminant if the object was
declared with an explicit discriminant value.

type RT (D : DT := DT'First) is 
   record
      case D is
         when DT'First =>
            null;

         when others =>
            null;
      end case;
   end record;

declare
  R1 : RT; --yes, can be changed

  R2 : RT := (D => DT'Last);  --yes, can be changed

  R3 : RT (DT'First); --no, cannot be changed

  R4 : RT (DT'First) := (D => DT'First); --no, can't be changed
begin
  R1 := (D => DT'Last);    --OK
  R2 := (D => DT'First);   --OK
end;

Basically, whenever you explicitly specify the discrimiant value in
the declaration (i.e. the "subtype mark"), then that constrains the
object, so that its discriminant cannot be changed.

This allows the compiler to allocate only as much stack space as is
needed for those components that apply to that discriminant value.

Note that in the case of R3 and R4 above, it doesn't matter that the
type has a default discriminant.  The fact that the object was
declared with an explicit value constrains the (sub)type.



  reply	other threads:[~2003-03-07 16:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-07  7:09 Disriminant question prashna
2003-03-07  8:01 ` tmoran
2003-03-07 11:20   ` Lutz Donnerhacke
2003-03-07 17:59     ` tmoran
2003-03-07 22:32       ` Lutz Donnerhacke
2003-03-07 11:17 ` Lutz Donnerhacke
2003-03-07 16:46   ` Matthew Heaney [this message]
2003-03-12  9:25     ` Disriminant question - Waiting desperately for solution :-( prashna
2003-03-12 16:12       ` Gautier
replies disabled

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