我为什么要使用接口?
Why should I use interfaces?
每当我用 Fortran 编程时,我都会使用模块,而不必担心编写接口。
现在我正在编写要在 R 内部使用的 Fortran 代码。问题是子例程不能在模块内部,所以我 "have" 编写接口。如果我不写接口一切正常,但聪明的互联网人说我应该写接口。
谁能解释一下为什么?有什么好处?
主要好处是编译器可以执行参数检查。这样,它可以警告您编程错误,否则很难检测到。
接口的另一个好处是您可以捆绑函数。例如,考虑 sin
函数,它是为 real
和 complex
不同类型的参数定义的。
使用接口,您可以为不同类型的变量调用相同的函数。
对于现代 Fortran 的某些功能(例如函数指针),(通常)需要接口。
作为 ,接口块可用于提供通用标识符和允许某些编译器检查。
如果您使用接口块来创建通用标识符,您可能是在模块内这样做,所以在模块无关时编写此类块的主要原因是它们在范围内创建的显式接口它们发生的地方。正是这些允许那些编译器检查和与模块的使用关联不再发生。
但是,有时需要一个显式接口,而不是一个很好的:如果引用具有某些功能的过程,则需要一个显式接口以使您的代码符合 Fortran 标准。这些功能可以在 F2008、12.4.2.2
中找到
A procedure other than a statement function shall have an explicit interface if it is referenced and
- a reference to the procedure appears
(a) with an argument keyword (12.5.2), or
(b) in a context that requires it to be pure,
- the procedure has a dummy argument that
(a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE, or VOLATILE attribute,
(b) is an assumed-shape array,
(c) is a coarray,
(d) is of a parameterized derived type, or
(e) is polymorphic,
- the procedure has a result that
(a) is an array,
(b) is a pointer or is allocatable, or
(c) has a nonassumed type parameter value that is not a constant expression,
- the procedure is elemental, or
- the procedure has the BIND attribute.
除此之外,隐式接口就足够了,不需要 interface
块。但仍然有帮助。
在您的情况下,它仅适用于接口具有这种影响的各种 Fortran 组件之间的调用。所以,你并不总是需要编写接口,有些东西没有它们也能工作。
从上述处理接口特定用途的优秀答案中略微多 "philosophical" 的观点。
您声明了所有变量,不是吗?据推测,您这样做是为了保护自己免受拼写错误、意外误用和滥用变量的影响,并避免 reader 了解偶尔神秘的规则,以及其他原因。那你为什么不想对你所有的子程序做同样的事情呢?以上均适用。
每当我用 Fortran 编程时,我都会使用模块,而不必担心编写接口。
现在我正在编写要在 R 内部使用的 Fortran 代码。问题是子例程不能在模块内部,所以我 "have" 编写接口。如果我不写接口一切正常,但聪明的互联网人说我应该写接口。
谁能解释一下为什么?有什么好处?
主要好处是编译器可以执行参数检查。这样,它可以警告您编程错误,否则很难检测到。
接口的另一个好处是您可以捆绑函数。例如,考虑 sin
函数,它是为 real
和 complex
不同类型的参数定义的。
使用接口,您可以为不同类型的变量调用相同的函数。
对于现代 Fortran 的某些功能(例如函数指针),(通常)需要接口。
作为
如果您使用接口块来创建通用标识符,您可能是在模块内这样做,所以在模块无关时编写此类块的主要原因是它们在范围内创建的显式接口它们发生的地方。正是这些允许那些编译器检查和与模块的使用关联不再发生。
但是,有时需要一个显式接口,而不是一个很好的:如果引用具有某些功能的过程,则需要一个显式接口以使您的代码符合 Fortran 标准。这些功能可以在 F2008、12.4.2.2
中找到A procedure other than a statement function shall have an explicit interface if it is referenced and
- a reference to the procedure appears
(a) with an argument keyword (12.5.2), or
(b) in a context that requires it to be pure,- the procedure has a dummy argument that
(a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE, or VOLATILE attribute,
(b) is an assumed-shape array,
(c) is a coarray,
(d) is of a parameterized derived type, or
(e) is polymorphic,- the procedure has a result that
(a) is an array,
(b) is a pointer or is allocatable, or
(c) has a nonassumed type parameter value that is not a constant expression,- the procedure is elemental, or
- the procedure has the BIND attribute.
除此之外,隐式接口就足够了,不需要 interface
块。但仍然有帮助。
在您的情况下,它仅适用于接口具有这种影响的各种 Fortran 组件之间的调用。所以,你并不总是需要编写接口,有些东西没有它们也能工作。
从上述处理接口特定用途的优秀答案中略微多 "philosophical" 的观点。
您声明了所有变量,不是吗?据推测,您这样做是为了保护自己免受拼写错误、意外误用和滥用变量的影响,并避免 reader 了解偶尔神秘的规则,以及其他原因。那你为什么不想对你所有的子程序做同样的事情呢?以上均适用。