[comp.lang.ada] Side Effects

jmoody@DCA-EMS.ARPA (Jim Moody, DCA C342) (04/12/88)

As two correspondents have noted, Sying-Syang Liu is wrong to expect 
a function with side effects to be called only once when it is an
explicit initialization of a component of a record which is a 
component of an array.
But the situation is even worse than that.
The order in which the components are evaluated is not determined by
the language (LRM 3.2.1(15), last sentence).
So, whereas Liu expected 1,1,1,1,1,2 and got 1,2,3,4,5,6
the compiler could have (without being incorrect) have given 5,4,3,2,1,6
or 1,5,4,2,3,6 or . . . .
What this goes to show is that writing function with side effects is a very,
very risky business in Ada.  Actually, it's a risky business in any language,
but Ada makes the risk explicit.
                                           Jim.

hmj@tut.fi (Matti J{rvinen) (04/14/88)

In article <8804112114.AA03398@ajpo.sei.cmu.edu> jmoody@DCA-EMS.ARPA (Jim Moody, DCA C342) writes:
>The order in which the components are evaluated is not determined by
>the language (LRM 3.2.1(15), last sentence).

So there is an inconsistency here (?):
LRM 3.2 (13) states that multiple declaration
a,b : T := func;
is equivalent to
a : T := func;
b : T := func;
IN THIS ORDER.

LRM 3.9 (3) states that elaboration must occur in the order given.

In fact, LRM 3.2.1 (15) refers to elaboration of ONE object (e.g. record).
One should not apply it to multiple declaration, so summa summarum:
values must be 1,2,3,4,5,6, in this order.

>What this goes to show is that writing function with side effects is a very,
>very risky business in Ada.  Actually, it's a risky business in any language,
>but Ada makes the risk explicit.

No. Ada zeros the risk.



-- 
Hannu-Matti Jarvinen, Tampere University of Technology, Finland
hmj@tut.fi, hmj@tut.uucp, hmj@tut.funet (tut.ARPA is not the same computer).