comp.lang.ada
 help / color / mirror / Atom feed
* Question about library-level functions
@ 2012-12-15  3:38 ytomino
  2012-12-15  9:47 ` AdaMagica
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: ytomino @ 2012-12-15  3:38 UTC (permalink / 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.



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

end of thread, other threads:[~2012-12-18  3:18 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-15  3:38 Question about library-level functions ytomino
2012-12-15  9:47 ` 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

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