在Fortran 90中模拟名称空间 [英] Emulating namespaces in Fortran 90

查看:190
本文介绍了在Fortran 90中模拟名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran 90最麻烦的问题之一是缺少命名空间。在这个前面的问题中如何使用Fortran 90模块数据from Pete ,已经讨论过USE的主要问题就像Python中的从模块导入*那样行为:在模块中声明为公共的所有内容都按原样导入导入模块的范围内。没有前缀。这使得非常非常难以理解的是,在阅读一些代码时,给定的标识符来自哪里,以及是否仍然使用给定的模块。



可能的解决方案,在上面链接的问题中讨论过,是使用ONLY关键字来限制导入的标识符和来自哪里的文档,虽然当模块非常大时这非常非常单调乏味。保持较小的模块,并始终使用USE:ONLY是解决Fortran 9X中缺乏命名空间和限定前缀的潜在好策略。



是否有其他(不是必然更好)解决方法战略? Fortran 2k3标准是否提供了有关命名空间支持的所有内容?

唯一的解决方案是为过程,变量,常量等添加通用前缀,以避免名称空间冲突。



可以为所有实体添加前缀(所有公共实体似乎更多)

 模块常量

隐式无

real,parameter :: constants_pi = 3.14
real,parameter :: constants_e = 2.71828183

结束模块常量

缺点是增加了模块内部的代码冗长度。作为替代方案,可以使用命名空间前缀包装模块,例如建议此处

 模块常量_内部

隐式无

真实,参数:: pi = 3.14
real,parameter :: e = 2.71828183

end模块constants_internal

模块常量

使用constants_internal,only:&
constants_pi => pi,&
constants_e => e

结束模块常量

最后一项是对您,Stefano,建议。

即使我们接受冗长的情况,Fortran并不区分大小写的语言迫使我们在实体中使用相同的分隔符(_)名。在我们不使用强命名规则之前,将模块名称(作为前缀)与实体名称区分开来是非常困难的,例如,模块名称只是一个字。


One of the most troublesome issues with Fortran 90 is the lack of namespacing. In this previous question "How do you use Fortran 90 module data" from Pete, it has been discussed the main issue of USE behaving like a "from module import *" in Python: everything that is declared public in the module is imported as-is within the scope of the importing module. No prefixing. This makes very, very hard to understand, while reading some code, where a given identifier comes from, and if a given module is still used or not.

A possible solution, discussed in the question I linked above, is to use the ONLY keyword to both limit the imported identifiers and document where they come from, although this is very, very tedious when the module is very large. Keeping the module small, and always using USE : ONLY is a potentially good strategy to work around the lack of namespacing and qualifying prefixes in Fortran 9X.

Are there other (not necessarily better) workaround strategies? Does the Fortran 2k3 standard say anything regarding namespacing support?

解决方案

For me this is the most irritating Fortran feature related to modules. The only solution is to add common prefix to procedures, variables, constants, etc. to avoid namespace collisions.

One can prefix all entities (all public entities seems to be more appropriate) right inside the module:

module constants

  implicit none

  real, parameter :: constants_pi = 3.14
  real, parameter :: constants_e = 2.71828183

end module constants

Drawback is increased code verbosity inside the module. As an alternative one can use namespace-prefix wrapper module as suggested here, for example.

module constants_internal

  implicit none

  real, parameter :: pi = 3.14
  real, parameter :: e = 2.71828183

end module constants_internal

module constants

  use constants_internal, only: &
    constants_pi => pi, &
    constants_e => e

end module constants

The last is a small modification of what you, Stefano, suggested.

Even if we accept the situation with verbosity the fact that Fortran is not case-sensitive language force us to use the same separator (_) in entities names. And it will be really difficult to distinguish module name (as a prefix) from entity name until we do not use strong naming discipline, for example, module names are one word only.

这篇关于在Fortran 90中模拟名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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