Showing results for 
Search instead for 
Did you mean: 

Generic interface when the different argument is passed through a pointer


I want to have a generic interface for two procedures where the different argument is passed through a pointer. Here is the example:

module typer_class
use iso_fortran_env, only: real64
implicit none

type, abstract :: typer
end type
type, extends (typer) :: typer_real64
end type
type, extends (typer) :: typer_complex64
end type
interface fix_type
  module procedure fix_type_real64
  module procedure fix_type_complex64
end interface
private :: fix_type_real64


real(real64) function fix_type_real64(this, y)
type(typer_real64) :: this
complex(real64)    :: y
fix_type_real64 = real(y)
end function

complex(real64) function fix_type_complex64(this, y)
type(typer_complex64) :: this
complex(real64)       :: y
fix_type_complex64 = y
end function

end module
program ppal
use typer_class
implicit none
class(typer),          pointer :: p => null()
type(typer_real64),    target  :: r
type(typer_complex64), target  :: c
complex(real64)                :: z = (1._real64, 2._real64)

print*, fix_type(r, z) !OK
print*, fix_type(c, z) !OK
p => r
print*, fix_type(p, z) !ERROR
end program

The code cannot be compiled due to this error:

test_typer.f90(59): error #6284: There is no matching specific function for this generic function reference.   [FIX_TYPE]
print*, fix_type(p, z)

The reason why I am using a generic interface instead of a deferred procedure is that fix_type_real64 and fix_type_complex64 have different output type so they cannot fit in the same abstract interface.

I imagine that there is no way to do it, but any help would be appreciated.

Thanks in advance.


0 Kudos
0 Replies