comp.lang.ada
 help / color / mirror / Atom feed
From: Serge Robyns <serge.robyns@gmail.com>
Subject: Declaring constants for a abstract tagged type and concrete sub classes
Date: Mon, 6 Jul 2015 03:10:22 -0700 (PDT)
Date: 2015-07-06T03:10:22-07:00	[thread overview]
Message-ID: <0ade6abf-34c0-46e2-8bd9-d60a00056321@googlegroups.com> (raw)

Hi,

I'm new on Ada OO-programming and I'm facing this dilemma on a class hierarchy.

The idea is to have a root type with some common data parts and common interface and some concrete procedures and functions but to force to have real variables through subclasses.

As such I've therefore the following (using Get/Set as examples only):

package Abstract_Root is
   type T_Root is abstract tagged with private;
   procedure Set_XYZ (Self: in out T_Root; ABC : in Integer);
   function Get_XYZ (Self: in T_Root) return Integer;
private
   type T_Root is abstract tagged record
      XYZ : Integer;
   end record;
end Abstract_Root;

As I want this class hierarchy to be usable without access type is was planning to define No_Element constants.  However, the compiler refuses me to create constant variables, as the type is abstract.  This isn't a problem when the record has only a few components, but when there are a lot it is painful, as I was "hoping" to use the No_Element as a part of the No_Element of the subclasses.  This idea came from the Ada Containers.  And to be able to use No_Element in if statements like

Something along:
    
package Concrete is
   type T_Concrete is new T_Root with private;
   No_Element : constant T_Concrete;
   ....
private
   type T_Concrete is new T_Root with record
      ...
   end record;
   No_Element : constant T_Concrete := (Abstract_Root.No_Element, ...);
end Concrete;


and then:

    A_Concrete : T_Concrete;
    ...
    A_Concrete := A_List.Find (Expression);
       -- A_List could be a complex object with multiple components,
       -- including T_Concrete.
    if A_Concrete = No_Element then
       -- some error handling
    end if;


Any advise on how to handle this need in Ada?  I'll face this issue too in a few potential design patters I want to use (Proxy for example).  Moreover, this constant allows me to initialize variables at the moment of my choice;

Regards,
Serge


             reply	other threads:[~2015-07-06 10:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-06 10:10 Serge Robyns [this message]
2015-07-06 11:55 ` Declaring constants for a abstract tagged type and concrete sub classes G.B.
2015-07-06 13:49   ` Serge Robyns
2015-07-06 17:36     ` G.B.
2015-07-07 16:27       ` Serge Robyns
2015-07-07 17:21         ` G.B.
2015-07-07 19:51           ` Serge Robyns
2015-07-07 20:14             ` Simon Wright
2015-07-07 20:18             ` Jeffrey R. Carter
2015-07-06 15:42 ` Simon Wright
2015-07-06 16:35   ` Serge Robyns
2015-07-06 17:02     ` Bob Duff
2015-07-06 21:16       ` Serge Robyns
2015-07-06 17:30 ` gautier_niouzes
replies disabled

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