Fortran で拡張された型を使用すると、別のモジュールの型拡張から見えるようにプライベート コンポーネントが必要になります。
gcc4.7 と ifort の両方で、bName が初期型と拡張子の両方にあるため、次のコードはエラーになります。しかし、これは非公開であるため、別のモジュールの拡張ではアクセスできません。つまり、bar_type の bName をコメントアウトすると、非公開であるというエラーが発生します。
module foo
type :: foo_type
character(256),private :: bName = "foo"
contains
procedure :: pName => pName
end type
contains
subroutine pName(this)
class(foo_type), intent(in) :: this
print*,trim(this%bName)
end subroutine
end module
module bar
use foo, only : foo_type
type,extends(foo_type) :: bar_type
character(256),private :: bName = "bar"
contains
procedure :: pName => pName
end type
contains
subroutine pName(this)
class(bar_type), intent(in) :: this
print*,this%bName
end subroutine
end module
program test
use foo, only : foo_type
use bar, only : bar_type
type(foo_type) :: foo_inst
type(bar_type) :: bar_inst
call foo_inst%pName()
call bar_inst%pName()
end program
bar_type が foo_type と同じモジュールに含まれている場合、bName は bar_type からアクセスできます。つまり、次のコードがコンパイルされます。
module foo
type :: foo_type
character(256),private :: bName = "foo"
contains
procedure :: pName => pName
end type
type, extends(foo_type) :: baz_type
contains
procedure :: pName => pName_baz
end type
contains
subroutine pName_baz(this)
class(baz_type), intent(in) :: this
print*,trim(this%bName)
end subroutine
subroutine pName(this)
class(foo_type), intent(in) :: this
print*,trim(this%bName)
end subroutine
end module
program test
use foo, only : foo_type,baz_type
type(foo_type) :: foo_inst
type(baz_type) :: baz_inst
call foo_inst%pName()
call baz_inst%pName()
end program
最初の例で何が起こるべきかを知るために、標準を解析するのに苦労しています。