(j3.2006) Procedure(), pointer: subroutine vs. function and implicit typing
Tobias Burnus
burnus at net-b.de
Fri May 1 16:15:10 EDT 2009
Hello,
I encountered this while checking a patch to allow procedure pointer
components in gfortran, but it also mostly applies to procedure pointers
themselves. Richard Maine's reply to my question at comp.lang.fortran
just was that he sees the question, but does not feel he can answer it.
I talking about
PROCEDURE(), POINTER :: ptr
and especially about the problem of subroutine vs. function but also
about the type of the return value.
I am sure that this has been discussed before, but I fail to find the
relevant emails/interpretation requests/parts in the standard.
a) Implicit typing. Am I reading the standard correctly that for the
following example, "i%ptr" gets implicitly typed as REAL?
procedure() :: proc
type t
procedure(), pointer :: ptr
end type t
type(t) :: i
i%ptr => proc
y = i%ptr()
end
b) The example Richard mentioned - is the following valid?
external some_subroutine, some_function
procedure(),pointer :: proc_pointer
logical :: do_the_subroutine
if (do_the_subroutine) then
proc_pointer => some_subroutine
call proc_pointer(5)
else
proc_pointer => some_function
y = proc_pointer(7)
end if
end
And without the "if"s? In the standard (7.4.2.2) one finds the following
but it is not completely clear to me whether it applies:
"If proc-pointer-object has an implicit interface and is explicitly
typed or referenced as a function, proc-target shall be a function. If
proc-pointer-object has an implicit interface and is referenced as a
subroutine, proc-target shall be a subroutine."
For the if-free version, I'm inclined to say that 7.4.2.2 applies and
that it would be invalid - but for the if-version?
c) Is the following valid?
IMPLICIT NONE ! p is not implicitly typeable
real, external :: func
procedure(), pointer :: p
p => func
! However, p is not referred as function
end
And related - especially if (c) is valid:
d) Is the following valid?
implicit none
real, external :: func
procedure(), pointer :: p1 ! not implicitly typeable
procedure(REAL), pointer :: p2
real :: y
p1 => func ! (A)
p2 => p1 ! (B) Assign target of p1 (i.e. "func") to p2
y = p2()
end
To quote 7.4.2.2 again:
"If proc-pointer-object has an implicit interface and is explicitly
typed or referenced as a function, proc-target shall be a function. If
proc-pointer-object has an implicit interface and is referenced as a
subroutine, proc-target shall be a subroutine."
"If proc-target and proc-pointer-object are functions, they shall have
the same type; corresponding type parameters shall either both be
deferred or both have the same value."
Tobias
More information about the J3
mailing list