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,3ccb707f4c91a5f2 X-Google-Attributes: gid103376,public From: ka@socrates.hr.att.com (Kenneth Almquist) Subject: Invoking parental methods (was: Java vs Ada 95) Date: 1996/10/31 Message-ID: <5599k7$bjr@nntpa.cb.lucent.com>#1/1 X-Deja-AN: 193346400 references: <5536en$2vk@nntpa.cb.lucent.com> <1996Oct29.070337.1@eisner> organization: Lucent Technologies, Columbus, Ohio newsgroups: comp.lang.ada Date: 1996-10-31T00:00:00+00:00 List-Id: > In article <5536en$2vk@nntpa.cb.lucent.com>, ka@socrates.hr.att.com (Kenneth Almquist) writes: >> procedure p(param: access child_obj) is >> type super is access all parent_obj; >> begin >> ... >> p(super(param)); -- call the parent's p >> ... >> end p; > > Declaring a pointer type within p does not meet the requirement > of minimizing the change required when an intermediate generation > gets inserted and there are many such invocations. Why can't it > be declared at the top of the package ? Because of the accessibility rules, which are designed to prevent dangling references to local variables. The code below illustrates the problem that they are designed to solve. As the comment notes, Ada prevents a dangling reference from being created by raising Contraint_Error when an attempt is made to convert the argument to and Obj_Ptr. If you want to minimize the changes required when inserting an intermediate generation, declare "subtype Super is Parent_Ptr;" at the top of the package, and the declare "type Super_Ptr is access all Super;" in each of the individual routines. Kenneth Almquist procedure Outer is type Obj is record I : Integer; end record; type Obj_Ptr is access all Obj; Dangling_Reference : Obj_Ptr; procedure Create_Dangling_Reference(P : access Obj) is type T is access all Obj; P2 : T; P3 : Obj_Ptr; begin P2 := T(P); -- Legal. P3 := Obj_Ptr(P); -- Raises Constraint_Error. Dangling_Reference := P3; end Create_Dangling_Reference; procedure Inner is Data : aliased Obj; begin Create_Dangling_Reference(Obj'access); end Inner; begin Inner; -- Dangling_Reference would point to a nonexistent object if -- execution were to reach this point. Fortunately, execution -- does not reach this point because Constraint_Error is raised -- above. Dangling_Reference.I := 0; end Outer;