类型内的变量声明在 Fortran 中如何工作?
How do variable declarations within types work in Fortran?
我正在尝试学习 Fortran,但我发现那里的教程并不多(可能是因为它是一种古老的语言)。我发现的那些是模糊的和没有描述性的,并且随着我深入到更复杂的事情中,越来越难猜测所说的教程在说什么。
我当前的问题是创建类型。本教程包含示例,例如:
module m_shapes
implicit none
private
public t_square
type :: t_square
real :: side
contains
procedure :: area ! procedure declaration
end type
contains
! Procedure definition
real function area(self) result(res)
class(t_square), intent(in) :: self
res = self%side**2
end function
end module m_shapes
这编译得很好,所以我知道它有效。
当我尝试做类似这样的事情时:
program type_test
implicit none
type :: thingy(a)
real :: a
end type
end program
它不会编译错误,例如“出现在 (2) 的类型参数列表中的 (1) 的组件既没有 KIND 也没有 LEN 属性”
我找到的教程对类型的解释不够好,我试过 real,kind :: a = kind(0.0)
之类的东西,但无济于事。有人知道怎么回事吗?
提前致谢。
你没有解释,你到底想做什么。或者你的话不够清楚。我们可以告诉你为什么你的尝试会产生错误,但我不知道你到底想做什么。
也许你只是想要
program type_test
implicit none
type :: thingy
real :: a
end type
end program
没有 (a)
?这声明了一个具有一个组件的简单类型。但很难猜测这是否是您想要的以及您使用 (a)
.
尝试的结果
它没有声明任何该类型的变量。这是使用
完成的
type(thingy) :: var
语法
type :: thingy(a)
real :: a
end type
attempts to declare a parametrized derived type. These types can depend on a kind or length parameter. These parameters must be integers. If it is a kind parameter, it allows to declare variables of the type with varying values of these parameters. Then the kind of some components of the type (you know what kind is, right? Fortran 90 kind parameter ) 根据参数的值得到它们的种类。如果它是一个长度参数,它允许派生类型的一些数组或字符串组件的长度被参数化——在变量声明时设置。
出现在括号中的这些参数必须是整数部分,并且必须具有kind
或len
属性。
例如
type :: param_type(k,l)
integer, kind :: k
integer, len :: l
real(kind=k), dimension(l) :: array
end type param_type
type(param_type(kind(1.), 10)) :: o_sp_10
type(param_type(kind(1.d0), 20)) :: o_dp_20
参数的值是在 o_sp_10
和 o_dp_20
对象的声明期间设置的。不想多说了。
我正在尝试学习 Fortran,但我发现那里的教程并不多(可能是因为它是一种古老的语言)。我发现的那些是模糊的和没有描述性的,并且随着我深入到更复杂的事情中,越来越难猜测所说的教程在说什么。
我当前的问题是创建类型。本教程包含示例,例如:
module m_shapes
implicit none
private
public t_square
type :: t_square
real :: side
contains
procedure :: area ! procedure declaration
end type
contains
! Procedure definition
real function area(self) result(res)
class(t_square), intent(in) :: self
res = self%side**2
end function
end module m_shapes
这编译得很好,所以我知道它有效。
当我尝试做类似这样的事情时:
program type_test
implicit none
type :: thingy(a)
real :: a
end type
end program
它不会编译错误,例如“出现在 (2) 的类型参数列表中的 (1) 的组件既没有 KIND 也没有 LEN 属性”
我找到的教程对类型的解释不够好,我试过 real,kind :: a = kind(0.0)
之类的东西,但无济于事。有人知道怎么回事吗?
提前致谢。
你没有解释,你到底想做什么。或者你的话不够清楚。我们可以告诉你为什么你的尝试会产生错误,但我不知道你到底想做什么。
也许你只是想要
program type_test
implicit none
type :: thingy
real :: a
end type
end program
没有 (a)
?这声明了一个具有一个组件的简单类型。但很难猜测这是否是您想要的以及您使用 (a)
.
它没有声明任何该类型的变量。这是使用
完成的 type(thingy) :: var
语法
type :: thingy(a)
real :: a
end type
attempts to declare a parametrized derived type. These types can depend on a kind or length parameter. These parameters must be integers. If it is a kind parameter, it allows to declare variables of the type with varying values of these parameters. Then the kind of some components of the type (you know what kind is, right? Fortran 90 kind parameter ) 根据参数的值得到它们的种类。如果它是一个长度参数,它允许派生类型的一些数组或字符串组件的长度被参数化——在变量声明时设置。
出现在括号中的这些参数必须是整数部分,并且必须具有kind
或len
属性。
例如
type :: param_type(k,l)
integer, kind :: k
integer, len :: l
real(kind=k), dimension(l) :: array
end type param_type
type(param_type(kind(1.), 10)) :: o_sp_10
type(param_type(kind(1.d0), 20)) :: o_dp_20
参数的值是在 o_sp_10
和 o_dp_20
对象的声明期间设置的。不想多说了。