Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

function - Passing type bound procedures as arguments

I am trying to pass a type bound procedure as an argument to another subroutine. I want to know if this is possible in Fortran. Here is a code snippet that shows what I am trying to do .

module type_definitions 
 type test_type 
 integer :: i1, i2,i3
 contains 
   procedure :: add_integers_up 
 end type test_type
 contains 
   subroutine add_integers_up(this,i4,ans)
       class(test_type) :: this 
       integer :: i4,ans 
       ans = this%i1+this%i2+this%i3+i4
    end subroutine add_integers_up

subroutine print_result_of_subroutine(i4,random_subroutine) 
  integer :: i4,ans 

  interface 
     subroutine  random_subroutine(i1,i2) 
       integer:: i1,i2
     end subroutine random_subroutine
  end interface

  call random_subroutine(i4,ans) 
  write(*,*) ans 


end subroutine print_result_of_subroutine


end module type_definitions


program main 
   use type_definitions
   implicit none 
   integer :: i1,i2,i3,i4
   integer :: ans 
   type(test_type) :: test_obj

   i1 =1; i2=2; i3=3
   test_obj%i1 = i1 
   test_obj%i2 = i2 
   test_obj%i3 = i3 
   i4 = 4

   call print_result_of_subroutine(i4,test_obj%add_integers_up) 

    end program main

Is this possible to do in Fortran? I get a compiler error when I try to compile this code using ifort.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

test_obj%add_integers_up is not a procedure - it is a binding that happens to be to a procedure called add_integers_up. You cannot pass a binding as an actual argument.

If you want to pass the specific procedure that the binding is associated with, then pass the procedure! Hypothetically:

call print_result_of_subroutine(i4, add_integers_up)

But as other posters have noted, in your example code the interface of that procedure does not match the interface of the corresponding dummy argument in print_result_of_subroutine.

If test_obj%add_integers_up referred to an associated procedure pointer component (and the interface for that component matched what was being expected by the print_result_of_subroutine) then things would work as you appear to be expecting.

Note that Fortran 90 does not support type bound procedures (or procedure pointer components) - your code very much requires Fortran 2003.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...