From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: new type'class'() legal?
Date: 1996/12/03
Date: 1996-12-03T00:00:00+00:00 [thread overview]
Message-ID: <E1uErt.3Gx.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: x7vk9r0n0z5.fsf@pogner.demon.co.uk
Simon Wright (simon@pogner.demon.co.uk) wrote:
: The code below compiles fine on Gnat 3.07, various platforms, but
: fails on Academic AdaMagic and ObjectAda (the demo version) at the
: point indicated with "e has no possible interpretation as an
: expression of the expected type base'class".
: I hope Gnat is right?
Nope. If you check RM95 4.7(3) on qualified expressions, in T'(X),
X must resolve to be of type T (or a universal type that covers it).
In your example, "T" is base'class, and e is of type ext'class --
definitely not the same type.
The simple solution is to write:
p := new ext'class'(e);
The implicit conversion to base'class happens automatically
as part of an allocator -- per RM95 4.8(3), the designated
type (base'class) of the access type ("base_p") only needs to "cover"
the type of the qualified expression.
Note that a qualified expression is about the only context where
you don't get implicit conversion to a class-wide type. The reason
for this special case is that one of the points of a qualified
expression is to resolve overloading. If the qualified expression
allowed implicit conversion to a class-wide type, it would be less
useful in resolving overloading.
The fact that a qualified expression is also the way to create
an initialized allocator does muddy things up a bit ;-), but
that is why the name resolution rules for allocators allows
for the implicit conversion (as implied by the "covers" terminology).
: (in case you're wondering why I wrote it this way, the original code
: has derived types ext1, ext2 which are each the root of a tree of
: derived types, and which have sufficiently different properties that I
: wanted to make sure that the appropriate proc() was called).
: --
: package classp is
: type base is tagged null record;
: type base_p is access base'class;
: type ext is new base with null record;
: procedure proc (e : ext'class);
: end classp;
: package body classp is
: p : base_p;
: procedure proc (e : ext'class) is
: begin
: p := new base'class'(e);
: -------------------------^
: end proc;
: end classp;
-Tucker Taft stt@inmet.com http://www.inmet.com/~stt/
Intermetrics, Inc. Cambridge, MA USA
next prev parent reply other threads:[~1996-12-03 0:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-12-02 0:00 new type'class'() legal? Simon Wright
1996-12-03 0:00 ` Tucker Taft [this message]
1996-12-10 0:00 ` Paul Chardon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox