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,a5d76152c5cb8790 X-Google-Attributes: gid103376,public From: Francois Godme Subject: class-wide objects Date: 1998/12/07 Message-ID: <366B1190.C8142307@magic.fr>#1/1 X-Deja-AN: 419437799 Content-Transfer-Encoding: 7bit X-Client: Magic On Line [unknown@ppp10-93.magic.fr] Content-Type: text/plain; charset=us-ascii Organization: very little Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-12-07T00:00:00+00:00 List-Id: Sometimes, pointers are used in an application only because the programming language does not offer good support for dynamically sized objects. Ada has very good support for dynamically sized objects. Global objects and stack objects can be sized as late as the elaboration of their declaration. For global objects, it means size is fixed during elaboration of the program, for stack objects, it means size is fixed during entry into the procedure or function which declares them. Stack allocation has lots of advantages: it's fast and it's not prone to memory fragmentation and memory leak because it works on an all or nothing scheme. In the following examples, one can see that Ada supports well dynamic objects because array attributes and reading of record discriminant allow to gain information on the actual parameter. With this information, first, some subtypes are defined, then objects or types can be built on top of them. procedure P1 (S : in String; N : in Natural) is Local : String (S'Range); -- anonymous subtype subtype T_Local_String is String (S'Range); Local_Array : array (1 .. N) of T_Local_String; begin ... end P1; procedure P2 (S : in Square; -- Square is defined Page 3-35 RM Ada83 N : in Natural) is Local : Square (S.Side); -- Side is the discriminant of type Square subtype T_Local_Square is Square (S.Side); Local_Array : array (1 .. N) of T_Local_Square; begin ... end P2; Ada95 has introduced tagged objects and class-wide objects. Class-wide object types are here to achieve polymorphism. They serve as formal parameters or to define access types. A class-wide object is a dynamically sized object. It has the size of the actual tagged object parameter passed to the formal parameter. Since the introduction of class-wide objects, support for dynamically sized objects, as far as I know and understand Ada95, has dropped down. Correct me if I'm wrong. Ada95 support for dynamically sized objects is now incomplete to allow pointer-free code because Ada95 doesn't provide a mechanism to gain information on the actual tagged object suitable to define a subtype. Ada95 allows a local variable to be constrained by the actual tagged parameter but in an incomplete way: procedure P3 (S : in Point'Class; N : in Natural) is Local : Point'Class := S; -- But what if type point is limited ? begin ... end P3; An attribute to get the subtype of the actual tagged parameter would solve both problems. May be we could combine the two attributes 'Class and 'Tag together: procedure P4 (S : in Point'Class; N : in Natural) is Local : Point'Class (S'Tag); -- allocates an object with the tag of S subtype T_Local_Point is Point'Class (S'Tag); Local_Array : array (1 .. N) of T_Local_Point; begin ... end P4; What do you think ? Am I right or am I missing something ? Could the GNAT team provide an implementation defined attribute for this ? Francois Godme