From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,da3bc97b715b8d03 X-Google-Attributes: gid103376,public From: stt@houdini.camb.inmet.com (Tucker Taft) Subject: Re: new type'class'() legal? Date: 1996/12/03 Message-ID: #1/1 X-Deja-AN: 202100096 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: houdini.camb.inmet.com references: organization: Intermetrics, Inc. newsgroups: comp.lang.ada Date: 1996-12-03T00:00:00+00:00 List-Id: 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