From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: Elaboration Question
Date: Fri, 02 Jan 2015 07:49:13 -0700
Date: 2015-01-02T07:49:13-07:00 [thread overview]
Message-ID: <_3spw.995330$Fo3.305798@fx09.iad> (raw)
In-Reply-To: <Xeopw.842131$No4.595859@fx19.iad>
On 2015-01-01 8:27 PM, Hubert wrote:
> For a newcomer to Ada, elaboration is one of the toughest things to
> understand. My understanding is so, that if you have for instance a
> variable in the package body, say :
>
> A : Integer := 0;
>
> then this variable must be initialized at some point and that's what
> elaboration does.
> The problem then occurs with the order in which elaboration occurs in
> different package that depend on each other etc.
> There are pragmas for the programmer to take control of the elaboration
> sequence but I have the feeling that may complicate things. especially
> when you want to make a library that should be independent and that's
> supposed to be be released to others at some point.
>
> So may question is, is it better to just leave those variables
> unitialized and then initialize them later for instance:
>
> A : Integer;
>
> PROCEDURE Initialize IS
> BEGIN
> A:= 0;
> END Initialize;
>
> Would that be generally better?
No, that's definitely worse, and really doesn't fix anything. There is
nothing preventing someone from attempting to reference A, before
Initialize is called. Initializing during elaboration is safer also
because it happens only once, and via the environment task. Relying on
programmers to initialize globals via explicit calls is error prone, and
can even lead to concurrency problems if Initialize is called from more
than one place.
It's generally best to initialize global variables upon declaration, and
usually you don't even need to think about elaboration. If there is a
problem with elaboration, the compiler should tell you, and then you can
think about how to resolve the elaboration issue, via the pragmas.
Usually the error message from the compiler should give a good clue
about the cause of the problem, and how to resolve it.
Otherwise, typically the compiler does a good job of sorting out
elaboration order all on its own.
Brad
>
>
> Thanks
>
>
>
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> http://www.avast.com
>
next prev parent reply other threads:[~2015-01-02 14:49 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-02 3:27 Elaboration Question Hubert
2015-01-02 7:56 ` Simon Wright
2015-01-02 22:16 ` Randy Brukardt
2015-01-02 14:49 ` Brad Moore [this message]
2015-01-02 11:08 ` Mark Lorenzen
2015-01-02 13:35 ` David Botton
2015-01-02 15:11 ` sbelmont700
2015-01-02 21:57 ` Niklas Holsti
2015-01-06 2:33 ` Randy Brukardt
2015-01-06 8:17 ` Niklas Holsti
2015-01-06 20:06 ` Randy Brukardt
2015-01-03 9:45 ` Jean François Martinez
2015-01-03 9:50 ` Hubert
2015-01-03 18:43 ` Jean François Martinez
2015-01-04 1:57 ` Hubert
2015-01-04 12:05 ` Simon Wright
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox