什么是子模块,它们是如何使用的? [英] What are submodules and how are they used?

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

问题描述

我不太了解submodules的目的。我知道大多数编译器对它们的支持很少,但这个概念很有趣。我认为我理解了基本概念,但我所见过的所有例子(Fortran Wiki,Modern Fortran Explained,技术报告)都是简单的,使用完全相同的例子( point 在同一个文件中输入全部内容),并且在调用函数时不显示它们的实际用途。在什么情况下你想使用子模块?当你想使用子模块时,你是否包含使用语句?我真的很喜欢,如果有人可以提供一个例子。

解决方案

简单的答案是,当你想要代码的子模块以减少父模块或子模块中的代码。



您可能希望父模块或子模块中的源代码更少,因为父代的源代码也越来越多长。

在子模块之前,这样做的唯一方法是将源代码从模块移出到不同的模块或外部过程中。但是,如果要移动的源代码引用PRIVATE事件或要保留在原始模块中的组件,这并不总是可行的。子模块可以通过主机关联访问其父模块或子模块中声明的东西 - 源代码仍然可以像访问父代的物理源代码的一部分一样访问PRIVATE事物。



如果与处理器相关,也可能希望将源代码从父模块中分离出来,以避免编译级联。对模块的更改通常需要重新编译该模块(及其后代),然后重新编译使用该模块(及其后代)的所有程序单元,反复级联进一步重新编译,其中重新编译的程序单元本身就是模块。对子模块的更改通常只需要重新编译该子模块及其任何子模块。



子模块的层次特性也适用于分层代码排列 - 您不需要希望兄弟姐妹能够直接访问它们定义的实体和过程。



USE语句仅用于要访问的东西在由模块提供的范围内。您不能在USE语句中使用子模块(尽管模块中定义的INTERFACE过程可能在子模块中定义了它的主体)。

模块的子模块不使用父模块(它不能 - 这有点像试图使用自己的模块),也不需要 - 它已经通过主机关联访问模块中的东西。启动子模块程序单元的子模块语句标识它扩展的模块(也可能是另一个子模块)。在模块的源代码中没有任何东西可以告诉它有多少个子模块可以扩展它。


I don't quite understand the purpose of submodules. I know there's very little support for them in most compilers but the concept is interesting. I think I understand the basic concept but all the examples I've seen (Fortran Wiki, Modern Fortran Explained, the technical report) are simplistic, use exactly the same example (point type all within the same file) and don't show their actual use when calling the function. In what situations would you want to use submodules? When you want to use a submodule do you include a use statement? I'd really like if someone could provide an example.

解决方案

The simple answer is that you put code into submodules when you want to have less code in the parent module or submodule.

You might want to have less source code in the parent module or submodule because the source code for the parent is getting too long.

Before submodules, the only way this could be done was to move source code out of the module into a different module or external procedure. But this wasn't always possible if the source code to be moved referenced PRIVATE things or components that were to remain in the original module. Submodules can access the things declared in their parent module or submodule by host association - the source code can still access PRIVATE things in the same way that it could if it was still part of the physical source code of the parent.

You might also want to split source code out of the parent module in order to avoid compilation cascades, if relevant to your processor. Changes to a module typically require recompilation of that module (and its descendants), and then recompilation of all program units that use that module (and their descendants), repeatedly cascading to further recompilation where any recompiled program units are themselves modules. Changes to a submodule typically only require recompilation of that submodule and any of its descendant submodules.

The hierarchical nature of submodules may also suit a hierarchical code arrangement - where you do not want siblings at the same level of the hierarchy to be able to directly access the entities and procedures that they define.

The USE statement is only used where you want to access things in a scope that are provided by a module. You cannot "use" a submodule in a USE statement (though a procedure with its INTERFACE defined in a module may have its body defined in a submodule).

A submodule of a module does not USE the parent module (it cannot - that is a bit like a module trying to USE itself) and it doesn't need to - it already has access to the things in the module by host association. The submodule statement that starts a submodule program unit identifies the module (and perhaps another submodule) that it extends. There is nothing in the source of a module proper that tells it how many submodules there may be extending it.

这篇关于什么是子模块,它们是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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