comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: variant record and pointer
Date: Fri, 02 May 2003 19:00:02 GMT
Date: 2003-05-02T19:00:02+00:00	[thread overview]
Message-ID: <Sezsa.30643$J27.26373@nwrdny02.gnilink.net> (raw)
In-Reply-To: 67ac8b2c.0305020925.6c27576d@posting.google.com

This is your question, but have you considered using tagged types instead of
variant records? You could declare your shape types as follows:

   type Shape;
   type Pointer is access all Shape'Class;

   type Shape is abstract tagged
      record
         Ptr     : Pointer;
         Special : Unbounded_String;
      end record;

   type Circle is new Shape with
      record
         Diameter : Natural;
      end record;

   type Square is new Shape with
      record
         Side : Natural;
      end record;

   type Triangle is new Shape with
      record
         Edge_1,
         Edge_2,
         Edge_3 : Natural;
      end record;

This would allow you to derive additional shapes (for example, Rectangle)
later, without impacting existing code. Moreover, you could write overloaded
subprograms that can dispatch at runtime, e.g.

    function Perimeter( X : in Shape ) return Float is abstract;

    function Perimeter( X : in Circle ) return Float;
    function Perimeter( X : in Square ) return Float;
    function Perimeter( X : in Triangle ) return Float;

    -- ...
    Pointer s;
    --

    Len := Perimeter( s.all );
    -- This calls the right Perimeter function,
    -- even if we cannot determine the type of s.all until runtime.





      parent reply	other threads:[~2003-05-02 19:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-02 17:25 variant record and pointer Noivet
2003-05-02 17:44 ` Robert A Duff
2003-05-02 19:00 ` Frank J. Lhota [this message]
replies disabled

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