package body functionality 
Author Message
 package body functionality

the question might be a bit obscure but
considering the two package bodies below

package body A is
 variable: some_type:=call_some_function_outside_A;
end A;

package body B is
 variable: some_type;
begin
 variable:=call_some_function_outside_B;
end B;

I know that in B the variable will be assigned
every time a program using the package is run,
but is this also true for A, or will the variable in
this case only be assigned at compile time ?

saturdays usually makes me confused...

/ Staffan Dittmer



Wed, 12 Nov 2003 22:28:47 GMT  
 package body functionality

Quote:

> the question might be a bit obscure but
> considering the two package bodies below

> package body A is
>  variable: some_type:=call_some_function_outside_A;
> end A;

> package body B is
>  variable: some_type;
> begin
>  variable:=call_some_function_outside_B;
> end B;

> I know that in B the variable will be assigned
> every time a program using the package is run,
> but is this also true for A, or will the variable in
> this case only be assigned at compile time ?

call_some_function_outside_A has a body unknown to A,
and the results of that call could be time-variant,
such as computed differently on weekends.  A has no
way of knowing this, so the results cannot be computed
"at compile time".


Thu, 13 Nov 2003 01:09:55 GMT  
 package body functionality

Quote:

> package body A is
>  variable: some_type:=call_some_function_outside_A;
> end A;

> package body B is
>  variable: some_type;
> begin
>  variable:=call_some_function_outside_B;
> end B;

> I know that in B the variable will be assigned
> every time a program using the package is run,
> but is this also true for A, or will the variable in
> this case only be assigned at compile time ?

Non-static initialization expressions, such as function calls, are
evaluated during elaboration, which happens at run time. Both variables
will be initialized during elaboration at run time.

--
Jeff Carter
"You tiny-brained wipers of other people's bottoms!"
Monty python & the Holy Grail



Thu, 13 Nov 2003 01:22:32 GMT  
 package body functionality


Quote:

> > package body A is
> >  variable: some_type:=call_some_function_outside_A;
> > end A;

> > package body B is
> >  variable: some_type;
> > begin
> >  variable:=call_some_function_outside_B;
> > end B;

> > I know that in B the variable will be assigned
> > every time a program using the package is run,
> > but is this also true for A, or will the variable in
> > this case only be assigned at compile time ?

> Non-static initialization expressions, such as function calls, are
> evaluated during elaboration, which happens at run time. Both variables
> will be initialized during elaboration at run time.

Unless the compiler has a form of global optimization that determines
the result of performing the operation at compile time is the same as
evaluating
both at elaboration time (a nice optimization).

SteveD

- Show quoted text -

Quote:

> --
> Jeff Carter
> "You tiny-brained wipers of other people's bottoms!"
> Monty Python & the Holy Grail



Thu, 13 Nov 2003 02:31:55 GMT  
 package body functionality

Quote:

> the question might be a bit obscure but
> considering the two package bodies below

> package body A is
>  variable: some_type:=call_some_function_outside_A;
> end A;

> package body B is
>  variable: some_type;
> begin
>  variable:=call_some_function_outside_B;
> end B;

> I know that in B the variable will be assigned
> every time a program using the package is run,
> but is this also true for A, or will the variable in
> this case only be assigned at compile time ?

> saturdays usually makes me confused...

The function of the compiler is to convert (in this case) package body A into
object code, not to execute the code in some other package (which, conceivably,
might not even be compiled yet).

--
        -- Aron

NB: To reply by e-mail, remove "spam-block." from my address.
- - - - - - - - - - -
"Nobody suffers the pain of birth or the anguish of loving a child in order for
presidents to make wars, for governments to feed on the substance of their
people, for insurance companies to cheat the young and rob the old."

        -- Lewis Lapham



Thu, 13 Nov 2003 03:16:07 GMT  
 package body functionality
If you're asking "are these different," the only difference I can think of
is that a given compiler *might* optimize them differently, up to and
including eliminating the variable altogether!


: the question might be a bit obscure but
: considering the two package bodies below
:
: package body A is
:  variable: some_type:=call_some_function_outside_A;
: end A;
:
: package body B is
:  variable: some_type;
: begin
:  variable:=call_some_function_outside_B;
: end B;
:
: I know that in B the variable will be assigned
: every time a program using the package is run,
: but is this also true for A, or will the variable in
: this case only be assigned at compile time ?
:
: saturdays usually makes me confused...
:
: / Staffan Dittmer



Thu, 13 Nov 2003 21:50:39 GMT  
 package body functionality

Quote:

> If you're asking "are these different," the only difference I can think of
> is that a given compiler *might* optimize them differently, up to and
> including eliminating the variable altogether!

There could be other differences because of the order of actions,

e.g.

package body A is
  variable: some_type:=call_some_function_outside_A;
  V2 : some_type := variable;
end A;
&
package body B is
  variable: some_type;
  V2 : some_type := variable;  -- default/uninitialised value of
variable
begin
  variable:=call_some_function_outside_B;
end B;

The second option also allows you to include an exception handler to
catch any exception raised by function outside B

--
hwyl/cheers,
Philip Anderson
Alenia Marconi Systems
Cwmbran, Cymru/Wales



Sat, 15 Nov 2003 17:15:54 GMT  
 package body functionality

Quote:

>package body A is
> variable: some_type:=call_some_function_outside_A;
>end A;
..
>but is this also true for A, or will the variable in
>this case only be assigned at compile time ?

Oddly enough, we had a very long discussion here a few months back about what it
would take for a compiler to be able to call a user-specified function at
compile time. If you're interested in that topic, you may want to try to look it
up at http://groups.google.com . I doubt we will recover all the better points
here again.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html



Sat, 15 Nov 2003 21:26:42 GMT  
 package body functionality

Quote:

> the question might be a bit obscure but
> considering the two package bodies below

> package body A is
>  variable: some_type:=call_some_function_outside_A;
> end A;

> package body B is
>  variable: some_type;
> begin
>  variable:=call_some_function_outside_B;
> end B;

> I know that in B the variable will be assigned
> every time a program using the package is run,

This is seriously wrong! B.variable is _not_ set many times. It
would be interesting to understand how you got that impression.

B.variable will be set _once_, at "elaboration" time. Elaboration
happens just before the main Ada program is run. All packages in the
program are elaborated, in an order determined by the compiler.

Note that if a function is called before its package is elaborated,
the exception Program_Error is raised; this is before your main
program gets to start.

To be safe, package A should have a 'pragma Elaborate' for the package
containing 'some_function_outside_A', and similarly for B.

Quote:

> but is this also true for A, or will the variable in this case only
> be assigned at compile time ?

As for B, A.variable is set at elaboration time.

--
-- Stephe



Sun, 16 Nov 2003 03:11:30 GMT  
 package body functionality

Quote:


>> package body B is
>>  variable: some_type;
>> begin
>>  variable:=call_some_function_outside_B;
>> end B;

>> I know that in B the variable will be assigned
>> every time a program using the package is run,

>This is seriously wrong! B.variable is _not_ set many times. It
>would be interesting to understand how you got that impression.

He said every time the *program* is run, which is quite correct. I'll admit the
possiblity that he meant something else (although I'm having trouble mentally
concoting a reasonable alternative), but that's not what he wrote.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html



Sun, 16 Nov 2003 04:34:52 GMT  
 package body functionality
Possible exception: If the package is a shared library or in a distributed
environment. I'm not sure what the language rules are, but I'd suspect that
a DLL would not re-elaborate every time another program started up and
called for its services. Maybe under some conditions (separate data spaces)
but not if it was some sort of shared resource. Or would it?

Interesting question - maybe I'll have to look up "Elaboration" tomorrow if
nobody posts an answer before I get back to work. :-)

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution

Web:      http://www.mcondic.com/


Quote:
> He said every time the *program* is run, which is quite correct. I'll
admit the
> possiblity that he meant something else (although I'm having trouble
mentally
> concoting a reasonable alternative), but that's not what he wrote.



Sun, 16 Nov 2003 05:29:11 GMT  
 package body functionality

Quote:

>Possible exception: If the package is a shared library or in a distributed
>environment. I'm not sure what the language rules are, but I'd suspect that
>a DLL would not re-elaborate every time another program started up and
>called for its services. Maybe under some conditions (separate data spaces)

My understanding is that DLL's keep a count of how many programs are using them,
and get automagicly unloaded when the count goes down to 0. You'd want it to
elaborate once for the first call that loads it in, then not again until the
next call after it unloads.

However, DLL's have their own special entry point as well. I suspect what Ada
DLL's have to do is implement that call (exported with the right linkage of
course) to call the standard Ada elaboration entry point. Perhaps someone who
has done this can comment.

Anyway, I wouldn't really call this an "exception". Its just that in this case
the "program" in question is the DLL itself, not its clients. It works if you
hold your head the right way. :-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html



Sun, 16 Nov 2003 21:46:53 GMT  
 package body functionality

Quote:



> >> package body B is
> >>  variable: some_type;
> >> begin
> >>  variable:=call_some_function_outside_B;
> >> end B;

> >> I know that in B the variable will be assigned
> >> every time a program using the package is run,

> >This is seriously wrong! B.variable is _not_ set many times. It
> >would be interesting to understand how you got that impression.

> He said every time the *program* is run, which is quite correct. I'll admit the
> possiblity that he meant something else (although I'm having trouble mentally
> concoting a reasonable alternative), but that's not what he wrote.

Hmm, I guess I read "subprogram from the package", rather than
"program using the package". Sorry!

--
-- Stephe



Sun, 16 Nov 2003 22:23:22 GMT  
 package body functionality
Point taken. It is kind of a matter of how you look at it. I just brought it
up because its an example of the importance of looking over the
documentation for unusual cases. In 99.44% of the cases, you've got a
simple, self contained, program that elaborates when you run it. But there
may be cases where the elaboration has more subtle implications.

I'll try to hold my head right and look at it with the corner of my eyes
next time. It's the only way to see something that has an SEP field
surrounding it. :-)

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution

Web:      http://www.mcondic.com/


Quote:
> Anyway, I wouldn't really call this an "exception". Its just that in this
case
> the "program" in question is the DLL itself, not its clients. It works if
you
> hold your head the right way. :-)



Sun, 16 Nov 2003 22:25:26 GMT  
 package body functionality

Quote:

> To be safe, package A should have a 'pragma Elaborate' for the package
> containing 'some_function_outside_A', and similarly for B.

A pragma Elaborate_All is almost always better than pragma Elaborate,
because it is transitive.

- Bob



Mon, 17 Nov 2003 09:37:47 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Package body over several files...

2. Won't allow package body.

3. Package spec and body in C.

4. package body T is

5. Ada95 Package Bodies: Required?

6. Visibility of packages in child bodies...

7. CPU timer package, body for VAX/VMS (4 of 5)

8. CPU Timer package, body for DOS machines (3 of 5)

9. null package bodies in Ada 9X

10. Help: Subprogram calls before package body elaboration.

11. CPU Timer package, body for Unix (5 of 5)

12. email package - how to get message body

 

 
Powered by phpBB® Forum Software