Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29487 Discussions

Procedure pointers declared private in Derived types

Debanjan_Mukherjee
790 Views
Hello,
I have a question on using proedure pointers in derived types in Fortran. I present my problem using a dummy example code here as follows:
type NewType
private
procedure(), pointer:: m_P
double precision:: m_Data
contains
procedure, public:: PFunc
end type NewType
Now I wish to be able to associate this procedure pointer m_P to any function, say, otherFunc(). I was wondering how I could do it.
The current solution I have, is to create a subroutine called setPointerFunction() that takes in an input procedure pointer to another procedure and associates it with m_P. And then, we can access the value of the function through PFunc(). But there seems to be some mistake I have made with the Pass and Nopass attributes. And I am not very sure whether I am doing this correct.
If you need any more info on my problem, please let me know.
Any help will be appreciated.
Thanks
Debanjan
0 Kudos
1 Reply
reinhold-bader
New Contributor II
790 Views
Hello Debanjan,

Since you declare the procedure pointer with a pair of empty brackets "()", it is for a procedure with an implicit interface. While this may give you a certain kind of flexibility on what subroutines can be invoked, it also is potentially error-prone and (more important in this context) has many restrictions: A lot of "modern" Fortran features require an explicit interface. In this case you are tripped up by the fact that, by default, your procedure pointer must use an object of "class(NewType)" as its first argument, which in turn (since it is polymorphic) implies the need for an explicit interface. You could get around this specific issue by declaring the procedure pointer with the "NOPASS" attribute, but depending on what variant of arguments you want to use you still may run into trouble.

I'd generally recommend to not use an implicit interface for procedure pointers in this kind of type definition, unless you also have a type component which enables proper dispatch.

Regards
Reinhold
0 Kudos
Reply