comp.lang.ada
 help / color / mirror / Atom feed
From: "Ehud Lamm" <mslamm@mscc.huji.ac.il>
Subject: Re: Heterogenous_Array_Test: please explain error
Date: Tue, 12 Jun 2001 22:44:22 +0300
Date: 2001-06-12T22:44:22+03:00	[thread overview]
Message-ID: <9g5rsc$2h0$1@news.huji.ac.il> (raw)
In-Reply-To: mailman.992374395.5423.comp.lang.ada@ada.eu.org

The approach is ok. The problem is that you must tell the compiler that the
class-wide pointer points to an object of a type that has the spefici
component you ask for: since this component is not part of the ancestor
type, and tus isn't part of the 'class type.
Notice that since Ada tries to check as much as possible before runtime it
falgs such a use as problematic, unless you explicitly say that the object
is of the specific type (see below). Usually such conversions/castings  are
not good oop style (use dispatching instrad).

Below is a simple example I use to cover these issues, the operations in the
package are trivial put statements  so I am not including them here.

HTH

package try_tagged_pack is

-- We define an abstract base "class" shape
type shape is abstract tagged null record;
-- and an abstract procedure that will be dispatcahble
procedure draw(s: in out Shape) is abstract;
procedure any_shape(s:in out shape'class);
procedure any2(s: in out shape);

type square is new Shape with
   record
      side:integer;
   end record;
procedure draw(s: in out square);


type circle is new shape with
   record
      radius:integer;
   end record;
procedure draw(c:in out circle);



 type ptr is access shape'class;
   type ptr2 is access circle;
   p:ptr;
   q:ptr2;
   s:square;
   c:circle;
  -- We can not declare variables of an abstract type.
  -- sh:shape;

  -- Nor can we declare a varible of a classwide type (it is unconstraind)
  -- unless we use initialization to a specific type (like s:string:="ehud")
  --sh:shape'class;
begin

   draw(s);
   c.radius:=5;
   p:= new circle'(radius=>5);
   q:=new circle;
   q.radius:=5;
   -- p is not constrained enough
  --- p.radius:=5;
  -- one possible solution (expalin why problematic!):
  circle(p.all).radius:=5;

   draw(p.all);
   -- We invoke any_shape on some variables of different types.
   any_shape(s);
   any_shape(c);
   any_shape(p.all);
   -- Comapre any_shape and any2
   any2(s);
   any2(c);
   any2(p.all);
end;


--
Ehud Lamm   mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <==  Me!








  reply	other threads:[~2001-06-12 19:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-12 20:32 Heterogenous_Array_Test: please explain error M. A. Alves
2001-06-12 19:44 ` Ehud Lamm [this message]
2001-06-12 19:50 ` Ted Dennison
2001-06-13  9:52   ` Lutz Donnerhacke
2001-06-13 14:42     ` Ted Dennison
2001-06-13 14:48       ` Ehud Lamm
2001-06-13 15:40         ` Ted Dennison
2001-06-13 17:10           ` Ehud Lamm
replies disabled

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