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

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

问题描述

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

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.

在我上面链接的问题中讨论的一个可能的解决方案是使用 ONLY 关键字来限制导入的标识符和它们来自的文档,尽管当模块非常大时这非常非常乏味.保持模块小,并始终使用 USE : ONLY 是解决 Fortran 9X 中缺少命名空间和限定前缀的潜在好策略.

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.

还有其他(不一定更好)的解决方法吗?Fortran 2k3 标准是否说明了有关命名空间支持的任何内容?

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

推荐答案

有几年的纯 Fortran 编程经验(我一年前才接触 Python),有一段时间不知道命名空间这样的概念.所以我想我学会了只跟踪导入的所有内容,正如高性能标记所说,只使用你有时间做的尽可能多的事情(乏味).

Having several years of Fortran-only programming experience (I got into Python only a year ago), I was not aware of such concept as namespaces for a while. So I guess I learned to just keep track of everything imported, and as High Performance Mark said, use ONLY as much as you have time to do it (tedious).

我能想到的另一种模拟命名空间的方法是将模块中的所有内容声明为派生类型组件.Fortran 不允许您以与命名空间相同的方式命名模块,但在模块名称前加上 module_ 前缀可能足够直观:

Another way I can think of to emulate a namespace would be to declare everything within a module as a derived type component. Fortran won't let you name the module the same way as the namespace, but prefixing module_ to module name could be intuitive enough:

MODULE module_constants
IMPLICIT NONE

TYPE constants_namespace
  REAL :: pi=3.14159
  REAL ::  e=2.71828
ENDTYPE

TYPE(constants_namespace) :: constants

ENDMODULE module_constants


PROGRAM namespaces
USE module_constants
IMPLICIT NONE

WRITE(*,*)constants%pi
WRITE(*,*)constants%e

ENDPROGRAM namespaces

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

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