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

procedures bound to a type

galtay
Beginner
512 Views
Hello, I'm having some trouble seeing what I'm doing wrong trying to bind a procedure to a user defined type. The example I wrote is below. I want to be able to write several functions that I will later associate with an extension of base_par_type. In fact, I just define base_par_type to have a base class to give to the spline function in the kernel module. The way I understand it, is that if I declare the par argument of the function spline as a class then I should be able to pass in any type derived from base_par_type. Now, when I go on to define my_par_type which is an extension of base_par_type and attempt to bind the spline function to it the compiler complains about the type of the dummy argument.

: error #8262: The passed-object dummy argument must be dummy data object with the same declared type as the type being defined. [PAR]

setting the nopass option for the bound procedure fixes things, but I should be able to bind the spline function with pass enabled shouldn't I?

I'm using the compiler on linux.
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20091012 Package ID: l_cprof_p_11.1.059

thanks,
Gabe



module base_par_class
type base_par_type
real :: h
end type base_par_type
end module base_par_class


module kernel_funcs
use base_par_class
contains
function spline(par,r) result(W)
class(base_par_type) :: par
real :: r
W = r
end function spline
end module kernel_funcs


module my_par_class
use kernel_funcs
type, extends(base_par_type) :: my_par_type
contains
procedure, pass :: kernel => spline
end type my_par_type
end module my_par_class


program test
use my_par_class
type(my_par_type) :: mypar
real :: r
mypar%h = 1.0

end program test
0 Kudos
2 Replies
Hirchert__Kurt_W
New Contributor II
512 Views
: error #8262: The passed-object dummy argument must be dummy data object with the same declared type as the type being defined. [PAR]

setting the nopass option for the bound procedure fixes things, but I should be able to bind the spline function with pass enabled shouldn't I?

No, not directly. The error message means exactly what it says -- in order to bind spline into my_par_type with the pass attribute, spline would need a dummy argument with declared type my_par_type, and spline doesn't have such a dummy argument.

In effect, any procedure being bound into a type with the pass attribute must be defined in the same scoping unit as the type definition, because that's the only way the function can see the type definition (to have a dummy argument of the type) and the type can see the function (to bind it into the type).

You could approximate the effect you are trying to achieve by defining in my_par_class a function that has a dummy argument of the right type and that uses spline to compute its result, and then binding that function into my_par_type. E.g., the function could be

function my_par_spline(par,r) result(W)
class(my_par_type) :: par
real :: r
W = spline(par,r)
end function spline

and you could bind my_par_spline to kernel instead of spline.

Does that help?

-Kurt
0 Kudos
galtay
Beginner
512 Views
Quoting - hirchert

No, not directly. The error message means exactly what it says -- in order to bind spline into my_par_type with the pass attribute, spline would need a dummy argument with declared type my_par_type, and spline doesn't have such a dummy argument.

In effect, any procedure being bound into a type with the pass attribute must be defined in the same scoping unit as the type definition, because that's the only way the function can see the type definition (to have a dummy argument of the type) and the type can see the function (to bind it into the type).

You could approximate the effect you are trying to achieve by defining in my_par_class a function that has a dummy argument of the right type and that uses spline to compute its result, and then binding that function into my_par_type. E.g., the function could be

function my_par_spline(par,r) result(W)
class(my_par_type) :: par
real :: r
W = spline(par,r)
end function spline

and you could bind my_par_spline to kernel instead of spline.

Does that help?

-Kurt

thanks Kurt, that does answer my question. Basically I was wondering if it was possible to bind a procedure in one module to a type in another module. Your work around is a good idea and amounts to just making wrapper routines in the module that defines the type which can be changed to call different procedures and still keeps the spline function isolated from the type. I wonder about speed when adding another function call, but hopefully the compiler can optimize it.
0 Kudos
Reply