comp.lang.ada
 help / color / mirror / Atom feed
* Q about finalization and interfaces.
@ 2007-12-13 12:20 Peter C. Chapin
  2007-12-13 17:49 ` Adam Beneschan
  0 siblings, 1 reply; 4+ messages in thread
From: Peter C. Chapin @ 2007-12-13 12:20 UTC (permalink / raw)


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?

Thanks in advance!

Peter



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q about finalization and interfaces.
  2007-12-13 12:20 Q about finalization and interfaces Peter C. Chapin
@ 2007-12-13 17:49 ` Adam Beneschan
  2007-12-13 19:15   ` Randy Brukardt
  0 siblings, 1 reply; 4+ messages in thread
From: Adam Beneschan @ 2007-12-13 17:49 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q about finalization and interfaces.
  2007-12-13 17:49 ` Adam Beneschan
@ 2007-12-13 19:15   ` Randy Brukardt
  2007-12-13 23:30     ` Peter C. Chapin
  0 siblings, 1 reply; 4+ messages in thread
From: Randy Brukardt @ 2007-12-13 19:15 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> wrote in message
news:190da31a-82f4-4ece-a71b-ed04213c92a4@d4g2000prg.googlegroups.com...
...
> 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.

I agree with Adam; I can't imagine why the Do_Stuff that prints a message is
not called, nor what is being called instead. Must be a compiler bug of some
sort.

                   Randy.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Q about finalization and interfaces.
  2007-12-13 19:15   ` Randy Brukardt
@ 2007-12-13 23:30     ` Peter C. Chapin
  0 siblings, 0 replies; 4+ messages in thread
From: Peter C. Chapin @ 2007-12-13 23:30 UTC (permalink / raw)


Randy Brukardt wrote:

> I agree with Adam; I can't imagine why the Do_Stuff that prints a message is
> not called, nor what is being called instead. Must be a compiler bug of some
> sort.

Okay, I wondered about that possibility. I'll look into sending in a
report. Thanks!

Peter



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-12-13 23:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-13 12:20 Q about finalization and interfaces Peter C. Chapin
2007-12-13 17:49 ` Adam Beneschan
2007-12-13 19:15   ` Randy Brukardt
2007-12-13 23:30     ` Peter C. Chapin

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