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,FREEMAIL_FROM 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 Received: by 10.68.59.229 with SMTP id c5mr17800057pbr.6.1321904413091; Mon, 21 Nov 2011 11:40:13 -0800 (PST) Path: lh20ni2591pbb.0!nntp.google.com!news1.google.com!postnews.google.com!h21g2000yqg.googlegroups.com!not-for-mail From: Yukicanis Newsgroups: comp.lang.ada Subject: Re: type access Parent'Class Date: Mon, 21 Nov 2011 11:40:12 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <7fd768a4-f97b-4045-8bab-49d2a3897a61@p2g2000vbj.googlegroups.com> NNTP-Posting-Host: 93.208.169.211 Mime-Version: 1.0 X-Trace: posting.google.com 1321904412 10639 127.0.0.1 (21 Nov 2011 19:40:12 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 21 Nov 2011 19:40:12 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: h21g2000yqg.googlegroups.com; posting-host=93.208.169.211; posting-account=PkAoygoAAABk9hjFSCyiskOHSFxak1Yv User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HNKUARELSC X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1,gzip(gfe) Xref: news1.google.com comp.lang.ada:18998 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-21T11:40:12-08:00 List-Id: On Nov 21, 8:25=A0pm, Adam Beneschan wrote: > On Nov 21, 11:03=A0am, Yukicanis wrote: > > > > > > > > > > > Dear Group, > > > I'm new to Ada and currently playing around with its OOP features. > > Now, I have the following source: > > > procedure Test is > > =A0 type Parent is tagged limited null record; > > =A0 type Parent_Access is access Parent'Class; > > =A0 A : access Parent'Class; > > =A0 B : Parent_Access; > > =A0 procedure Dyn_Disp(X : access Parent'Class) is > > =A0 begin > > =A0 =A0 A :=3D X; > > =A0 =A0 B :=3D X; > > =A0 end Dyn_Disp; > > begin > > =A0 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? > > > Thanks in advance. > > Ada is a strongly typed language. =A0That means that you can have two > types that look exactly the same but are still different types, > because you've declared them as different types, and you can't just > use them interchangeably. =A0Example: > > =A0 type Acc_Type_1 is access all Integer; > =A0 type Acc_Type_2 is access all Integer; > =A0 V1 : Acc_Type_1; > =A0 V2 : Acc_Type_2; > > =A0 V1 :=3D V2; =A0 =A0 -- ILLEGAL! > > This is illegal because V1 and V2 have different types, even though > the types look the same. =A0You have to use a type conversion: > > =A0 V1 :=3D Acc_Type_1(V2); =A0-- LEGAL > > This doesn't have anything to do with access types, by the way. =A0All > types in Ada are like that. > > In your original example, "access Parent'Class" is an anonymous type, > and every time an anonymous type occurs, it's a new type. =A0So that's > why > > =A0 B :=3D X; > > is illegal. =A0You have to use a type conversion: > > =A0 B :=3D Parent_Access(X); > > That still won't quite work, because anonymous access types are > "general" access types and their values can point to any aliased > Parent'Class object. =A0However, Parent_Access is called a "pool- > specific" access type because it can only point to objects that are > allocated with "new". =A0To make Parent_Access a general access type, > add an "all": > > =A0 type Parent_Access is access all Parent'Class; > > and now your example will compile. =A0In my view, there isn't much > reason *not* to use "all" in an access type declaration, and I've > gotten in the habit of including it routinely. =A0(But you can't use > "all" like this if it's not in a TYPE declaration.) > > The language allows > > =A0 A :=3D X; > > even though A and X have different types, because there's no way to > write a type conversion. =A0This is an exception to the strong-typing > rules. > > Hope this helps, > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Adam Yes, that helps me a lot! That you very much! BTW, I just discovered that B :=3D A.all'Access; also seems to work (with access all). But won't the rigth hand side result in an anonymous type again? Thanks in advance.