comp.lang.ada
 help / color / mirror / Atom feed
* controlled initialization
@ 2002-12-16  7:04 Maxim Reznik
  2002-12-16 14:33 ` Hyman Rosen
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Maxim Reznik @ 2002-12-16  7:04 UTC (permalink / raw)


Hi!
Can you help me?
I'm stuck with following example.

I expected statement
  return Statement'(Ada.Finalization.Controlled with C => 0);
created object of type Statement,
then Initialyze procedure would be called for it and
Put_Line printed "Init".

But when I compile program with GNAT 3.15p for Linux
it doesn't happen.

May be it's bug of GNAT?

Thank you

Maxim Reznik

------------------------------------
with Ada.Finalization;
package Tests is

    type Statement is new Ada.Finalization.Controlled with record
       C : Integer;
    end record;

    procedure Initialize (Item : in out Statement);

    subtype Class is Ada.Finalization.Controlled'Class;

    function Create_Statement return Class;

end Tests;

------------------------------------
with Ada.Text_IO;
package body Tests is

    procedure Initialize (Item : in out Statement) is
    begin
       Ada.Text_IO.Put_Line ("Init");
    end Initialize;

    function Create_Statement return Class is
    begin
       return Statement'(Ada.Finalization.Controlled with C => 0);
    end Create_Statement;

end Tests;

------------------------------------
with Tests;
procedure Test is

    use Tests;

    Object : Class := Create_Statement;
begin
    null;
end Test;




^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: controlled initialization
@ 2002-12-17  6:20 Grein, Christoph
  0 siblings, 0 replies; 11+ messages in thread
From: Grein, Christoph @ 2002-12-17  6:20 UTC (permalink / raw)


> Interesting. So (remembering that I don't know Ada) the following
> would be illegal as well? If it is, is there a way to do what the
> code obviously intends? Do I understand the issue correctly, that
> is that MakeT could use O?
> 
> package P is
>    type T is private;
>    O : constant T;
> private
>    type T is new Controlled with null record;
>    function MakeT return T;
>    O : constant T := MakeT;
> end P;

Absolutely correct, MakeT is not elaborated when you call it here. So this code is not illegal, but will raise an exception.

And why would you want to call a function here? A record initialisation can 
easily be used because you have full visibility of all components.

In a contrived case like the following, you would have a point. But then simply 
define your complicated function in a child package, use an Elaborate_Body 
pragma and call the function to initilise O. Here you are - no problem.

package P is
   type T is private;
   O : constant T;
private
   type T is new Controlled with record
     Comp: Some_Type;
   end record;
   O : constant T := (Controlled with Comp => Some very complicated code here);
end P;



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

end of thread, other threads:[~2002-12-17  7:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-16  7:04 controlled initialization Maxim Reznik
2002-12-16 14:33 ` Hyman Rosen
2002-12-16 15:06   ` Robert A Duff
2002-12-16 23:26   ` Matthew Heaney
2002-12-17  1:50     ` Hyman Rosen
2002-12-17  2:00       ` Bill Findlay
2002-12-17  7:55       ` Simon Wright
2002-12-17  6:59   ` Maxim Reznik
2002-12-16 23:31 ` Stephen Leake
2002-12-16 23:31 ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
2002-12-17  6:20 Grein, Christoph

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