I am programming with FORTRAN oop features. Now I have a subroutine which takes another subroutine as its argument. But I want the subroutine takes unlimited polymorphic subroutine as the argument as well as normal subroutine. For example I have:
Questions:
[ol 1]
[li]I am only allowed to pass an unlimited polymorphic subroutine into "PassFunc". I am not be able to pass a normal function (a function without class(*)). Is there any way to make "PassFunc" take other types of functions? (Example: Func1 works but Func2 doesn't. I got access violation with IVF, though it didn't complain when compiling. Is it possible to make it work? If it is possible, I can make use of other subroutine without modifying.)[/li]
[li]In the case, the type of "FuncRes" variable depends on "MyInput". Now the only way I know is to use a nested select type. But in fact, there is no need to do this since "FuncRes" and "MyInput will always be the same type. Is there a way to reduce the nested select type? (It would be a disaster if I have many intermediate variables.)[/li]
[/ol]
Thanks for any suggestions.
Code:
subroutine PassFunc(MyFunc, MyInput)
class(*), intent(inout) :: MyInput
interface
subroutine MyFunc(A, B)
class(*), intent(in) :: A
class(*), intent(out) :: B
endsubroutine MyFunc
endinterface
class(*), allocatable :: FuncRes
select type(MyInput)
type is(real(8))
allocate(real(8)::FuncRes)
select type(FuncRes)
type is(real(8))
call MyFunc(MyInput, FuncRes)
MyInput = MyInput + FuncRes**2
endselect
type is(complex(8))
endselect
endsubroutine PassFunc
!Input Functions
subroutine Func1(A, B)
class(*), intent(in) :: A
class(*), intent(out) :: B
select type(A)
type is(real(8))
select type(B)
type is(real(8))
B = A + 1
endselect
type is(complex(8))
select type(B)
type is(complex(8))
B = A - 1
endselect
endselect
endsubroutine Func1
subroutine Func2(A, B)
real(8), intent(in) :: A
real(8), intent(out) :: B
B = A + 1
endsubroutine Func2
[ol 1]
[li]I am only allowed to pass an unlimited polymorphic subroutine into "PassFunc". I am not be able to pass a normal function (a function without class(*)). Is there any way to make "PassFunc" take other types of functions? (Example: Func1 works but Func2 doesn't. I got access violation with IVF, though it didn't complain when compiling. Is it possible to make it work? If it is possible, I can make use of other subroutine without modifying.)[/li]
[li]In the case, the type of "FuncRes" variable depends on "MyInput". Now the only way I know is to use a nested select type. But in fact, there is no need to do this since "FuncRes" and "MyInput will always be the same type. Is there a way to reduce the nested select type? (It would be a disaster if I have many intermediate variables.)[/li]
[/ol]
Thanks for any suggestions.