From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3f267467312181f1 X-Google-Attributes: gid103376,public From: matthew_heaney@acm.org (Matthew Heaney) Subject: Re: problem in changing Discriminants from access type Date: 1998/05/11 Message-ID: #1/1 X-Deja-AN: 352434419 Content-Transfer-Encoding: 8bit References: <35577418.0@news1.ibm.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Organization: Network Intensive Newsgroups: comp.lang.ada Date: 1998-05-11T00:00:00+00:00 List-Id: In article <35577418.0@news1.ibm.net>, "Gil Kaspi" 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.all := (False, F => 5.0); -- (illegal) 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.