comp.lang.ada
 help / color / mirror / Atom feed
From: ytomino <aghia05@gmail.com>
Subject: Question about library-level functions
Date: Fri, 14 Dec 2012 19:38:20 -0800 (PST)
Date: 2012-12-14T19:38:20-08:00	[thread overview]
Message-ID: <f7a79646-eb34-42c6-b1df-ccbb8c59437f@googlegroups.com> (raw)

Hello.

What's the difference between a library-level(top-level?) function(A) and a function(B) in a library-level package?

with Something;
function A return access T; -- returns new allocated value

with Something;
package P is
   function B return access T; -- same as above
end P;

I had thought these are same.
But, returned value from A seems that's finalized inside A.
B is not.

Does the master of a library-level function happen to exist inside itself?




My test case is below:

---- %< ----

with Ada.Finalization;
package lifetime is
   type T is limited private;
   function Create return T;
private
   type T is limited new Ada.Finalization.Limited_Controlled with null record;
   overriding procedure Finalize (Object : in out T);
end lifetime;

with Ada.Text_IO;
with System.Address_Image;
package body lifetime is
   procedure Finalize (Object : in out T) is
   begin
      Ada.Text_IO.Put_Line ("Finalize (" & System.Address_Image (Object'Address) & ")");
   end Finalize;
   function Create return T is
   begin
      return Result : T := (Ada.Finalization.Limited_Controlled with null record) do
         Ada.Text_IO.Put_Line ("Initialize (" & System.Address_Image (Result'Address) & ")");
      end return;
   end Create;
end lifetime;

with lifetime;
function alloc return access lifetime.T;

function alloc return access lifetime.T is
begin
   return new lifetime.T'(lifetime.Create);
   -- lifetime.Finalize may be called here
end alloc;

with Ada.Text_IO;
with lifetime;
with alloc;
procedure main is
begin
   Ada.Text_IO.Put_Line ("before");
   declare
      type A is access all lifetime.T;
      P : A := A(alloc);
   begin
      -- P.all may be already finalized!!!
      Ada.Text_IO.Put_Line ("lifetime");
   end;
   Ada.Text_IO.Put_Line ("after");
end main;

---- >% ----

% gnatmake -gnat2012 main.adb && ./main
before
Initialize (0000000100100090)
Finalize (0000000100100090) <- !!!!
lifetime
after

When I tried to move 'alloc' to inside of any package, the calling lifetime.Finalize in 'alloc' would not be invoked. It's natural.



             reply	other threads:[~2012-12-15  3:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-15  3:38 ytomino [this message]
2012-12-15  9:47 ` Question about library-level functions AdaMagica
2012-12-15 10:50   ` ytomino
2012-12-15 11:38     ` AdaMagica
2012-12-17 19:49       ` Adam Beneschan
2012-12-18  2:26         ` ytomino
2012-12-15 15:23 ` sbelmont700
2012-12-16  6:09   ` ytomino
2012-12-16  9:43 ` Simon Wright
2012-12-16 10:21   ` AdaMagica
2012-12-16 13:07     ` ytomino
2012-12-16 18:31     ` Simon Wright
2012-12-18  3:18       ` ytomino
2012-12-18  0:07     ` Randy Brukardt
2012-12-16 13:34   ` ytomino
2012-12-16 15:54     ` AdaMagica
2012-12-18  0:09       ` Randy Brukardt
replies disabled

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