为什么我应该使用接口? [英] Why should I use interfaces?

查看:174
本文介绍了为什么我应该使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在Fortran编程时,我都会使用模块,而且我不必担心编写接口。

现在我正在编写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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆