comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Generic body
Date: Thu, 15 May 2008 07:59:34 -0700 (PDT)
Date: 2008-05-15T07:59:34-07:00	[thread overview]
Message-ID: <511b5387-c94f-4293-91f0-d8a4c4447894@u12g2000prd.googlegroups.com> (raw)
In-Reply-To: 482C0178.5020501@gmail.com

On May 15, 2:25 am, Sébastien <seb.mor...@gmail.com> wrote:

> I read a lot of stuff about elaboration, I'm not a master of the subject
> yet, but I understand a bit about it. I thought that Elaborate_All was
> about dynamic check vs static check? (flat -gnatE for circular dependancy)

I don't think so.  It's about elaboration order, and making sure that
variables have a chance to get initialized before they're used.  If
you have a package body:

   package body Pak2 is
      Counter : Integer := 0;
      function Next_Counter return Integer is
      begin
         Counter := Counter + 1;
         return Counter;
      end Next_Counter;
   end Pak2;

In Ada semantics, the elaboration of Pak2's body is what initializes
Counter to 0.  But if the elaboration of some other package (say Pak3)
used Next_Counter before Pak2's body had a chance to be elaborated,
Counter would be uninitialized and Next_Counter would return garbage.
So it's necessary to ensure that Pak2's body is elaborated before Pak3
is elaborated, and that's what the Elaborate pragmas are for.
(Without the Elaborate pragmas, Pak2's body might be elaborated first
anyway, but you can't be sure.)

That's how it works in Ada.  I know that GNAT has a big long chapter
in the manual describing how it determines the elaboration order, but
I'm not really familiar with the details.  But I'd use the pragmas
anyway; even if GNAT would get things right without them, you may want
to port to a different compiler at a later time.

In any case, I like Gautier's solution and I wish I'd thought of it;
that avoids the Elaboration issues completely.

                               -- Adam



  reply	other threads:[~2008-05-15 14:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-14 18:26 Generic body Sébastien
2008-05-14 21:49 ` Randy Brukardt
2008-05-14 21:51 ` Samuel Tardieu
2008-05-14 22:03 ` Adam Beneschan
2008-05-15  9:25   ` Sébastien
2008-05-15 14:59     ` Adam Beneschan [this message]
2008-05-15 15:40       ` Sébastien
2008-05-15  8:43 ` gautier_niouzes
2008-05-15 13:11   ` Sébastien
replies disabled

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