为什么我应该使用接口? [英] Why should I use interfaces?
问题描述
现在我正在编写Fortran代码以便在R中使用。问题是子例程不能位于模块内部,因此我需要编写接口。如果我不写界面一切正常,但智能互联网的人说我应该写接口。
有人能解释我为什么吗?有什么好处?
主要好处是编译器可以执行参数检查。这样,它可以警告你编程错误,否则将很难发现。
另一个关于接口的好处是可以捆绑函数。例如,考虑 sin
函数,它定义了 real
和 complex
不同种类的参数。
使用接口,您可以为不同类型的变量调用相同的函数。
对于现代Fortran的某些功能(如函数指针),接口(通常)是必需的。
Whenever I program in Fortran I use modules and I don't have to worry about writing interfaces.
Now I'm writing Fortran code to use inside R. The problem is that the subroutines cannot be inside a module so I "have" to write interfaces. If I don't write the interface everything works fine, but smart internet people said I should write the interfaces.
Can someone explain me why? What are the benefit?
The main benefit is that the compiler can perform argument checks. This way, it can warn you about programming errors, which would be very hard to detect otherwise.
Another very nice thing about interfaces is that you can bundle functions. Consider for example the sin
function, which is defined for real
and complex
arguments of different kinds.
Using interfaces, you can call the same function for different types of variables.
For some features of modern Fortran such as function pointers, interfaces are (usually) required.
这篇关于为什么我应该使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!