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

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

问题描述

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

Whenever I program in Fortran I use modules and I don't have to worry about writing interfaces.

现在我正在编写要在 R 中使用的 Fortran 代码.问题是子例程不能在模块中,所以我必须"编写接口.如果我不写接口一切正常,但聪明的互联网人说我应该写接口.

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.

关于接口的另一个非常好的事情是你可以捆绑函数.例如,考虑定义的 sin 函数用于不同类型的 realcomplex 参数.使用接口,您可以为不同类型的变量调用相同的函数.

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.

对于现代 Fortran 的某些特性,例如函数指针,(通常)需要接口.

For some features of modern Fortran such as function pointers, interfaces are (usually) required.

这篇关于为什么要使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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