In file mod_xyz.f90:9
type(ilist), pointer :: next => null()
1
Error: Initialization of pointer at (1) is not allowed in a PURE
procedure
Two questions:
1) who is right?
2) if this is forbidden, why is it so? Or in other words, what is the
difference between a type definition in a subroutine and one placed
before the CONTAINS (apart from the scope)?
Thank you!
Marco
module mod_xyz
implicit none
contains
pure subroutine psub()
type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist
end subroutine psub
end module mod_xyz
Pure functions and subroutines should not have side effects. An
initialisation
like the above means that there is a side effect:
if ( .not. associated(next) ) then
! Only run the first time the routine is called
allocate( next )
...
endif
...
This means the routine is doing something different the first time it
is called and that is in conflict with the Fortran definition of
"pure".
I would say that gfortran and g95 are right to complain, but the error
message is probably not required by the standard. The somewhat lame
argument is that your program is non-conformant, so the compiler may
do what it pleases ...
Regards,
Arjen
Arjen,
thank you, I see your argument for a local variable:
pure subroutine psub()
type(xyz), pointer :: next => null()
end subroutine psub
but in my example the initialization is part of a type definition, so
it doesn't require a special handling at the first execution (or does
it, nevertheless?). What I don't see is why this is fine:
module mod_xyz
implicit none
type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist
contains
pure subroutine psub()
type(ilist) :: var
end subroutine psub
end module mod_xyz
but this is not
module mod_xyz
implicit none
contains
pure subroutine psub()
type ilist
type(ilist), pointer :: next => null()
integer :: i
end type ilist
type(ilist) :: var
while there are restrictions with respect to avoiding side effects,
these apply to default initialization via a type declaration statement
(or data statement) which would imply the SAVE attribute, and
therefore constitute a side effect.
However, default initialization of type components does not, even
if you go on to declare
type(ilist) :: x
within psub (and as long as you do not default initialize x).
So I think gfortran and g95 are incorrectly refusing the code.
Apart from this: Unless you absolutely know that you're only ever going
to use ilist inside psub, it is probably a better idea to put the type definition
into the specification part of the module from the point of view
of potential reuse, even if only by other module procedures in mod_xyz.
Regards
Reinhold
m schrieb:
Oops, I missed that part! Yes, Reinhold is quite probably right:
gfortran and
g95 erroneously complain here.
The solution would be to either put the definition outside the pure
routine
or to avoid the default initialisation - it should not be necessary as
now
it can only be used inside the pure routine and therefore you have
full control
over its correct use.
Regards,
Arjen
Arjen, Reinhold, OK, thank you.
Marco