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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,dfdb3e9be36e818e X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.35.131 with SMTP id h3mr18890925pbj.1.1321904003406; Mon, 21 Nov 2011 11:33:23 -0800 (PST) Path: lh20ni2567pbb.0!nntp.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news-out.readnews.com!transit3.readnews.com!panix!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: type access Parent'Class Date: Mon, 21 Nov 2011 14:33:22 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <7fd768a4-f97b-4045-8bab-49d2a3897a61@p2g2000vbj.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 X-Trace: pcls6.std.com 1321904002 13861 192.74.137.71 (21 Nov 2011 19:33:22 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Mon, 21 Nov 2011 19:33:22 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:NPrI8ggIVYAtpvY/BbGIc6iZ+9c= Xref: news2.google.com comp.lang.ada:14483 Content-Type: text/plain; charset=us-ascii Date: 2011-11-21T14:33:22-05:00 List-Id: Yukicanis writes: > Dear Group, > > I'm new to Ada and currently playing around with its OOP features. > Now, I have the following source: > > procedure Test is > type Parent is tagged limited null record; > type Parent_Access is access Parent'Class; > A : access Parent'Class; > B : Parent_Access; > procedure Dyn_Disp(X : access Parent'Class) is > begin > A := X; > B := X; > end Dyn_Disp; > begin > null; > end Test; > > When I try to complie that with GNAT 4.6.1 I get the following error > message: > > test.adb:9:10: expected type "Parent_Access" defined at line 3 > test.adb:9:10: found type access to "Parent'Class" defined at line 6 > > which I don't really understand since type "Parent_Access" is type > access to Parent'Class, isn't it? No, there are three different access types -- Parent_Access, the anonymous type of A, and the anonymous type of X. They're all "access to Parent'Class", but they are distinct. There are some implicit conversion rules that make "A := X;" legal. But there is no implicit conversion to Parent_Access, so "B := X;" is illegal. You normally want access-to-class-wide types (and many other access types) to be "general", which is specified by adding "all": type Parent_Access is access all Parent'Class; If you do that, then you could say "B := Parent_Access(X);" or "B := A.all'Access;". But I suggest you avoid anonymous access types, except in certain special cases. They are confusing. I also suggest you put most of your code in packages, rather than in the main procedure. That will also help avoid confusion, because things behave differently when inside procedures. For example, you can't have any dispatching procedures unless you put the type in a package spec. - Bob