Re: Scope of implied-do index



Steven G. Kargl wrote:
ISTR reading in the F95 standard that the scope of the index
of an implied-do is limited to the implied-do. I can't locate
where I would have gotten that impression, so a pointer to
the F95 text would be much appreciated.

In particular, I interested in what the standard says about

integer i
real x(10)
do i = 1, 10
x = (/ (i, i=1, 10) /)
end do



I'm looking at the F2003 draft, but that should also apply to F95(*). In 16.3, I
read:

"The name of a data-i-do-variable in a DATA statement or an ac-do-variable in an
array constructor has a scope of its data-implied-do. It is a scalar variable
that has the type and type parameters that it would have it were the name of a
variable in the scoping unit that includes the DATA statement or array
constructor, and this type shall be integer type; it has no other attributes.
The appearance of a name as a data-i-do-variable of an implied-DO in a DATA
statement or an ac-do-variable in an array constructor is not an implicit
declaration of a variable whose scope is the scoping unit that contains the
statement."

From my reading of this, the i in the array constructor does not have any effect
on the value of i in the enclosing loop. However, the i in the array constructor
gets its type because of the preceding "integer i" declaration.

If we were instead to have:

integer i
real x(10)
do i = 1, 10
x = (/ (j, j=1, 10) /)
end do

....then j now gets its type from implicit typing -- and it ends up as an
integer. Furthermore, with

integer i
real x(10)
do i = 1, 10
x = (/ (y, y=1, 10) /)
end do

....then y again gets its type from implicit typing, and ends up as a real. From
the paragraph I quote above, the ac-do-variable type must be integer type, so
this last 'program' should cause a compiler to barf. Sure enough, both my
compilers (Lahey & Intel) spit out an error.

cheers,

Rich


.



Relevant Pages

  • Re: NAG, Sun, Compaq possible f95 bug
    ... > implied-DO in a DATA statement or an array constructor has the ... This also seems to imply that default implicit typing is in effect, ...
    (comp.lang.fortran)
  • Re: Scope of implied-do index
    ... in a DATA statement or an array constructor has the scope of the implied-DO list. ... It is a scalar variable that has the type and type parameters that it would have if it were the name of a variable in the scoping unit that includes the DATA statement or array constructor, and this type shall be integer type." ... Aside from the fact that they're not included in the above quote, consider section 14.7.5, which includes "Beginning of execution of the action specified by an implied-DO list in an input/output statement causes the ... section 9.4.2 requires that the variable for an i/o implied-do be a scalar variable; this restriction is not there for array-constructor or data implied-do loops. ...
    (comp.lang.fortran)
  • Re: Array Constructors
    ... in an array constructor, is its value always restored afterwards. ... array constructor has a scope that is just the implied-do. ... That was my initial thought, but IVF complaining if *a* variable, i, ...
    (comp.lang.fortran)
  • Re: Scope of implied-do index
    ... of an implied-do is limited to the implied-do. ... "The name of a variable that appears as the DO variable of an implied-DO in a DATA statement or an array constructor has the scope of the implied-DO list. ... section 9.4.2 requires that the variable for an i/o implied-do be a scalar variable; this restriction is not there for array-constructor or data implied-do loops. ... The text for input/output implied-do loops is similar, but includes the additional requirement that the variables in nested input/output implied-do loops shall not be associated -- which distinction makes sense with the fact that their scope is beyond that of the implied-do loop; the association would be irrelevant if they're not really the same variable as the one in the outer context. ...
    (comp.lang.fortran)
  • Re: Array Constructors
    ... Can anyone confirm that when a variable is used in an implied do loop ... in an array constructor, is its value always restored afterwards. ... array constructor has a scope that is just the implied-do. ... so to think about 'restoring' is not correct). ...
    (comp.lang.fortran)