comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Inheritance question...
Date: 1997/03/25
Date: 1997-03-25T00:00:00+00:00	[thread overview]
Message-ID: <E7Lry9.5n.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 33361956.4F30@pacbell.net


Marc Bejerano (mbejeran@pacbell.net) wrote:

: I have a package called foo...

: package foo is
:   type bar is tagged private;
:   type bar1 is new bar with private;

:   .
:   .
:   .

: private
:   type bar is tagged record
:     v: integer;
:   end record;

:   type bar1 is new bar with record
:     vv: character;
:   end record;
: end foo;

: when I try to create a descendent type in my main procedure I get:

:   "type extension at deeper accessibility level than parent"

This one should be in the FAQ...

: here's my "simplified" main procedure:

: with foo; use foo;

: procedure foofoo is
:   type my_bar is new bar1 with record
:     s: string(1..128);
:   end record;
: begin
: .
: .
: .
: end foofoo;

: what am I doing wrong?

The declarations inside a procedure are at a deeper "accessibility"
level than the declarations outside the procedure.  Accessibility
level is essentially the same thing as dynamic nesting level.
Packages don't increase the accessibility level, but functions,
procedures, tasks, and declare-blocks do.

When you extend a tagged type, you must do it at the same accessibility
level as the parent type.  Hence, you must move your type extension
into a package, which you "with" from your procedure foofoo.

The reason for this limitation is that a copy of an object of
your type extension "my_bar" might be created in the heap, and then
your procedure foofoo might exit.  If you had overridden any
primitive operations on the type, those overridings would
also be "gone" once foofoo exited.  The copy of the "my_bar" object
would effectively have dangling references to the primitive operations
declared inside foofoo.

The simplest rule to remember is never declare a tagged type,
including a type extension, inside a function or procedure.
Define types at the package level.

: TIA, Marc

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




      parent reply	other threads:[~1997-03-25  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-03-23  0:00 Inheritance question Marc Bejerano
1997-03-24  0:00 ` Robert A Duff
1997-03-24  0:00 ` Jerome Desquilbet
1997-03-25  0:00 ` Tucker Taft [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