Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic type-bound procedures with procedure arguments

I am trying to write a generic type-bound procedure that takes different callback functions as parameters. When compiling following code (with ifort 12.1.3) I get the warning below:

module test

type :: a_type
  contains
  procedure :: t_s => at_s
  procedure :: t_d => at_d
  generic :: t => t_s,t_d
end type a_type

abstract interface
  integer function cb_s(arg)
  real(4) :: arg
  end function cb_s

  integer function cb_d(arg)
  real(8) :: arg
  end function cb_d
end interface

contains

subroutine at_s(this,cb)
  class(a_type) :: this
  procedure(cb_s) :: cb 
end subroutine

subroutine at_d(this,cb)
  class(a_type) :: this
  procedure(cb_d) :: cb 
end subroutine

end module test

The warning:

compileme.f(27): warning #8449: The type/rank/keyword signature for this specific
procedure matches another specific procedure that shares the same generic
binding name.   [AT_D]

It seems like the compiler is not differentiating between different function interfaces when used as procedure arguments...

My question is: Why aren't those types checked and what is the correct, clean way to write generic type-bound procedures with procedures or procedure pointers as arguments?

Possible solution

As Vladimir F pointed out, only the callback function's return arguments are type checked. In my case it is OK to then just slightly change the functions' interfaces:

abstract interface
  real(4) function cb_s(arg)
  real(4) :: arg
  end function cb_s

  real(8) function cb_d(arg)
  real(8) :: arg
  end function cb_d
end interface
like image 433
Omar Awile Avatar asked Oct 08 '22 07:10

Omar Awile


1 Answers

The compiler is right, because Fortran 2008's 12.4.3.4.5 Restrictions on generic declarations has

Two dummy arguments are distinguishable if - one is a procedure and the other is a data object, - they are both data objects or known to be functions, and neither is TKR compatible with the other, - one has the ALLOCATABLE attribute and the other has the POINTER attribute, or - one is a function with nonzero rank and the other is not known to be a function.

It means that both your functions are integer functions so they are not distinguishable.

like image 199
Vladimir F Героям слава Avatar answered Oct 12 '22 12:10

Vladimir F Героям слава