comp.lang.ada
 help / color / mirror / Atom feed
* new type'class'() legal?
@ 1996-12-02  0:00 Simon Wright
  1996-12-03  0:00 ` Tucker Taft
  1996-12-10  0:00 ` Paul Chardon
  0 siblings, 2 replies; 3+ messages in thread
From: Simon Wright @ 1996-12-02  0:00 UTC (permalink / raw)



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?

(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;




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: new type'class'() legal?
  1996-12-02  0:00 new type'class'() legal? Simon Wright
@ 1996-12-03  0:00 ` Tucker Taft
  1996-12-10  0:00 ` Paul Chardon
  1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1996-12-03  0:00 UTC (permalink / raw)



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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: new type'class'() legal?
  1996-12-02  0:00 new type'class'() legal? Simon Wright
  1996-12-03  0:00 ` Tucker Taft
@ 1996-12-10  0:00 ` Paul Chardon
  1 sibling, 0 replies; 3+ messages in thread
From: Paul Chardon @ 1996-12-10  0:00 UTC (permalink / raw)
  To: Simon Wright


Hello,

I think that the parameter e 's got a value whose type is a subtype from
base'class . Since everything that belongs to the class wide type
ext'class belongs of course to the class wide type base'class.
Consequently, I belive gnat is right.

	Paul.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1996-12-10  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-12-02  0:00 new type'class'() legal? Simon Wright
1996-12-03  0:00 ` Tucker Taft
1996-12-10  0:00 ` Paul Chardon

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