comp.lang.ada
 help / color / mirror / Atom feed
From: matthew_heaney@acm.org (Matthew Heaney)
Subject: Re: problem in changing Discriminants from access type
Date: 1998/05/11
Date: 1998-05-11T00:00:00+00:00	[thread overview]
Message-ID: <matthew_heaney-ya023680001105982131020001@news.ni.net> (raw)
In-Reply-To: 35577418.0@news1.ibm.net


In article <35577418.0@news1.ibm.net>, "Gil Kaspi" <kaspi@ibm.net> wrote:

(start of quote)
procedure d_array is

 type Arr_Node;

type arr is access arr_node;

type Test is array (Integer range <>) of Integer;

subtype Int_Limits is Integer range -1000..1000;

type Int_Arr is array (Int_Limits range <>) of Integer;

type Arr_Node (Start : Int_Limits :=1; Ends : Int_Limits := 0) is
record
   Store : Int_Arr (Start..Ends);
end record;

-- declare --
D_Arr : Arr;

begin

-- create --
D_Arr := new Arr_Node'(Start => 1, Ends => 0, Store => (others => 0));

-- change --
D_Arr.all :=(Ends => Index, Start => Index , Store => (others =>1));

end;
----------------------------------------------------------------------------
------------------------

!!! first, writing this code without the access will work OK.
running this code will raise CONSTRAINT_ERROR.
the problem I think is hiding in the last line
(end of quote)


The reason you're getting CE is because you're trying to change the
discriminant of an aliased object (here, on the heap).  This is illegal. 
Here's why.

Consider this:

type R (D : Boolean := True) is
   record
      case D is
         when False =>
            F : Float;

        when True =>
            I : Integer;

      end case;
   end record;

type RA is access R;

subtype RAF is RA (False);
subtype RAT is RA (True);

declare
    O1 : constant RA := new RA'(True, I => 5);
    O2 : constant RAT := O1;
begin
   <O1 and O2 both designate an object whose discrim is True>
   O1.all := (False, F => 5.0);   -- (illegal)
   <O1 designates an object whose discrim is False, but what about O2???>

You see the problem.  By declaring an access subtype, you're guaranteeing
something about the object designated by the access object.  Here, O2,
because it is of subtype RAT, has a guarantee that it will always point to
an object whose discriminant is True.

If you were able to assign the object a new value with a different
discriminant (through an unconstrained access type, like RA), then that
would violate the invariant of all the access objects designating the
object with the original discriminant.

This rule also applies to statically declared objects (ie not on the heap)
that are declared aliased:

declare
   O : alised R;
begin

The statment
   O := (False, 5.0); 
is illegal, because it changes the value of the discriminant from True to False.

However, 

declare
   O : R;
begin
   O := (False, 5.0);

is perfectly legal, and will not raise CE.




  reply	other threads:[~1998-05-11  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-12  0:00 problem in changing Discriminants from access type Gil Kaspi
1998-05-11  0:00 ` Matthew Heaney [this message]
1998-05-11  0:00 ` Lowe Anthony A
1998-05-11  0:00   ` Matthew Heaney
1998-05-12  0:00 ` Anonymous
replies disabled

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