Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

fortran - What are submodules and how are they used?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...