comp.lang.ada
 help / color / mirror / Atom feed
* Side effect
@ 1988-04-09  2:05 Scott Liu
  1988-04-09  6:58 ` Richard A. O'Keefe
  0 siblings, 1 reply; 2+ messages in thread
From: Scott Liu @ 1988-04-09  2:05 UTC (permalink / raw)


Recently, I wrote an Ada program with side effect on a function, 
called NEXT, which is used on both type and variable declarations.
I got very weird program output when I ran it using TeleSoft Ada 
compiler.  Is this a compilation error or compiler-dependent feature?

Thanks for your comments.

Sying-Syang Liu
Dept. of Computer and Information Sciences
University of Florida
UUCP: {ihnp4,rutgers}!codas!ufcsv!lius
Internet mail: lius@ufl.edu


------ Program source -------------

with TEXT_IO; use TEXT_IO;
procedure SET2 is
  ID:INTEGER:=0;

  function NEXT return INTEGER is
  begin
    ID:=ID+1;
    return ID;
  end;

begin
  declare
    type BIG is record X:INTEGER:=NEXT; end record;  -- ???
    A:array (1..5) of BIG;                           -- ???
    B:integer:=NEXT;
  begin
    for I in A'RANGE loop
      PUT(INTEGER'IMAGE(A(I).X));
    end loop;
    PUT_LINE(INTEGER'IMAGE(B));
  end;
end;

----- Program output -----

 1 2 3 4 5 6

--------------------------
Note: I expected to have the output 
	1 1 1 1 1 2

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

* Re: Side effect
  1988-04-09  2:05 Side effect Scott Liu
@ 1988-04-09  6:58 ` Richard A. O'Keefe
  0 siblings, 0 replies; 2+ messages in thread
From: Richard A. O'Keefe @ 1988-04-09  6:58 UTC (permalink / raw)


In article <14425@uflorida.cis.ufl.EDU>, lius@beach.cis.ufl.edu (Scott Liu) writes:
>   ID: INTEGER := 0;
>   function NEXT return INTEGER is
>   begin
>     ID := ID+1;
>     return ID;
>   end;
> declare
>   type BIG is record X: INTEGER := NEXT; end record;  -- ???
>   A: array (1..5) of BIG;                             -- ???

{A is (1,2,3,4,5), not the expected (1,1,1,1,1).}

This is the way ADA is generally supposed to work.  For example,
	X, Y: INTEGER := NEXT;
is defined to be equivalent to
	X: INTEGER := NEXT;
	Y: INTEGER := NEXT;
In general, what matters is not how many copies of an initialiser you
write, but how many things are initialised:  an expression is evaluated
for _each_ variable it is used to initialise.  It's not the way I'd prefer
it, but at least it is consistent and predictable.

LRM 3.7 paras 5 and 11.
LRM 3.2.1 (note para 6).

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

end of thread, other threads:[~1988-04-09  6:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1988-04-09  2:05 Side effect Scott Liu
1988-04-09  6:58 ` Richard A. O'Keefe

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