use*_*806 6 fortran constants derived-types
似乎Fortran 90不允许派生数据类型中的命名常量.这是真的?以下代码不起作用.
program my_prog
implicit none
type :: my_type
integer, parameter :: a = 1
real(kind(1.d0)) :: b
end type my_type
type (my_type) :: complex_type
end program my_prog
Run Code Online (Sandbox Code Playgroud)
编译器说在派生类型定义中不允许使用参数语句.
当我删除parameter关键字一切正常.但是,我怎样才能确保组件a在其他地方没有被修改?
根据标准,不允许.的部件属性说明符可能只pointer,和dimensionFortran的90/95(第4.4.1节),另外allocatable在2003年的Fortran(第4.5.3节),并且另外codimension,和contiguous2008年的Fortran(节4.5.4.1).
你可以在这里获取文件.
我遇到了与说明target符类似的问题,这也是不允许的.
编辑:为什么不尝试private组件?
module typedef
type :: my_type
integer, private :: a_int = 1
real(kind(1.d0)) :: b
contains
procedure :: a
end type my_type
contains
function a(complex_type)
class(my_type),intent(in) :: complex_type
integer :: a
a = complex_type%a_int
end function
end module
program my_prog
use typedef
implicit none
type (my_type) :: complex_type
complex_type%b = 2.d0 ! This should work
write(*,*) complex_type%a(), complex_type%b
! complex_type%a_int = 3 ! This should fail
end program my_prog
Run Code Online (Sandbox Code Playgroud)