引数としての束縛手順を入力します
-
20-12-2019 - |
質問
次のように別の関数に(外部関数として)タイプの束縛手順を別の関数に渡したい:
module mod1
implicit none
type type1
real :: a
contains
procedure,pass :: f
end type
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) s(t%f)
contains
real function s(g)
real,external :: g
s=g(5e0)+2e0
end function
end program
.
gfortranが生産するこのエラーを与えます:
write(*,*) s(t%f)
1
Error: Expected argument list at (1)
.
しかし私ができることは:
program test
t%a=3e0
write(*,*) s(k)
contains
real function s(g)
real,external :: g
s=g(5e0)+2e0
end function
real function k(e)
real,intent(in) :: e
k=3e0+e
end function
end program
.
問題はタイプの範囲の手順議論として、私は瞬間に答えが私を助けることができるのか見ていません。
編集:
(うまくいけば)が難しさを示すより良い例:
module mod2
implicit none
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
end module
module mod1
use mod2
type type1
real :: a
contains
procedure,pass :: f
procedure,pass :: h
end type
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
real function h(y)
class(type1), intent(inout) :: y
h=s(y%f)
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) t%h
end program
.
編集II: OK、ラッパーはまだポインタと組み合わせて機能します。
module mod2
implicit none
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
end module
module mod1
use mod2
type type1
real :: a
contains
procedure,pass :: f
procedure,pass :: h
end type
class(type1),pointer :: help_w
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
real function h(y)
class(type1), intent(inout),target :: y
help_w => y
h=s(wrap)
end function
function wrap(x)
real,intent(in) :: x
wrap=help_w%f(x)
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) t%h()
end program
.
これは確かに美しい解決策ではなく、少なくともそれはうまくいきます。
解決
ラッパーを書くことができます。これは最も簡単なバージョンです。内部機能をダミー引数として渡す必要があります(F2008)、t
がそこにある場合はモジュールにラッパーを宣言することができます。
注意:s
のプロシージャ引数の宣言をより現代的なものに変更しました - インターフェースブロック。
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) s(wrap)
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
function wrap(x)
real, intent(in) :: x
wrap = t%f(x)
end function
end program
.
あなたのエラーの理由はリンクされた質問に対する回答でよく説明されています、あなたはあなたが試した方法でタイプの束縛手続きを渡すことはできません。
所属していません StackOverflow