Fortran extended types for different modules

With extended types in Fortran, there must be a private component visible to the type extension in another module.

When using gcc4.7 and ifort, the following code results in an error, since bName has both an initial type and an extension. But since it is private, it is not available in the extension in another module, that is, if you comment out bName in bar_type, you will get an error message that it is private.

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 

If bar_type was contained in the same module as foo_type, then bName will be accessible from bar_type, i.e. the following code will compile

 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 

It is not easy to disassemble standards to know what should happen in the first example.

+4
source share
1 answer

I believe the first example is not up to standard.

Despite the fact that the private attribute makes the bName component inaccessible to the external module foo , it is still inherited by bar_type (perhaps quite meaningless, since nothing can be done with it, but this is not a problem) - see note 4.51 in f2003:

Inaccessible components and parent type bindings are also inherited, but they remain inaccessible in the extended type. Inaccessible objects arise if access to a type that is accessed through the use of an association and has a private structure.

So, bar_type has an inherited component named bName , which makes it an error to add another component under this name (see clause 16.2 for the scope and name rules).

+2
source

All Articles