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,bb05655456eafe6d X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!postnews.google.com!d4g2000prg.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Q about finalization and interfaces. Date: Thu, 13 Dec 2007 09:49:51 -0800 (PST) Organization: http://groups.google.com Message-ID: <190da31a-82f4-4ece-a71b-ed04213c92a4@d4g2000prg.googlegroups.com> References: <4761239f$0$31576$4d3efbfe@news.sover.net> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1197568191 29483 127.0.0.1 (13 Dec 2007 17:49:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 13 Dec 2007 17:49:51 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d4g2000prg.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:18924 Date: 2007-12-13T09:49:51-08:00 List-Id: On Dec 13, 4:20 am, "Peter C. Chapin" wrote: > I'm trying to understand the interaction between controlled types, > interfaces, and class-wide dispatching. Accordingly I wrote the > following program, which I will present in sections. First the package > specification: > > with Ada.Finalization; > package Check_Package is > > type B is interface; > procedure Do_Stuff( Thing : in B ) is abstract; > > type D is new Ada.Finalization.Controlled and B with > record > X : Integer := 0; > end record; > > overriding procedure Do_Stuff( Thing : in D ); > overriding procedure Finalize( Thing : in out D ); > > end Check_Package; > > The idea is that I want to build a derivation class rooted on the B > interface with some of the types in that class (such as D) being > controlled. The corresponding body prints out a few messages to help > track what is happening. Note that I use the X component as a kind of > object ID number. > > with Ada.Text_IO; use Ada.Text_IO; > with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; > package body Check_Package is > > procedure Do_Stuff( Thing : in D ) is > begin > Put("Do_Stuff( Thing : in D ) => "); > Put(Thing.X); > New_Line; > end Do_Stuff; > > procedure Finalize( Thing : in out D ) is > begin > Put("Finalize( Thing : in out D ) => "); > Put(Thing.X); > New_Line; > end Finalize; > > end Check_Package; > > Now the test program that exercises the above code looks like this: > > with Check_Package; use Check_Package; > procedure Check is > Object : D; > Some_Thing : B'Class := Object; > begin > Object.X := -1; > Do_Stuff(Some_Thing); > end Check; > > Using GNAT GPL 2007 I get the following output: > > Finalize( Thing : in out D ) => 0 > Finalize( Thing : in out D ) => -1 > > My interpretation is that Object is copied (with the default ID value of > zero) when Some_Thing is initialized; Object's ID is then modified. Both > Object and its copy are finalized when the program ends. But... what > happened to my call to Do_Stuff?? I expected that call to dispatch on > Some_Thing and invoke the Do_Stuff for type D. What am I misunderstanding? You expect the compiler not to have any bugs. That's your misunderstanding. :) Seriously, I've looked over the rules carefully and I think Do_Stuff (the one that prints output) should be called. But I've been known to get things wrong sometimes. -- Adam