comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Q about finalization and interfaces.
Date: Thu, 13 Dec 2007 09:49:51 -0800 (PST)
Date: 2007-12-13T09:49:51-08:00	[thread overview]
Message-ID: <190da31a-82f4-4ece-a71b-ed04213c92a4@d4g2000prg.googlegroups.com> (raw)
In-Reply-To: 4761239f$0$31576$4d3efbfe@news.sover.net

On Dec 13, 4:20 am, "Peter C. Chapin" <pcha...@sover.net> 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





  reply	other threads:[~2007-12-13 17:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-13 12:20 Q about finalization and interfaces Peter C. Chapin
2007-12-13 17:49 ` Adam Beneschan [this message]
2007-12-13 19:15   ` Randy Brukardt
2007-12-13 23:30     ` Peter C. Chapin
replies disabled

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