Fortran

Guide To Learn

Renaming imported entities to avoid name conflict

Based on what we’ve covered so far about importing entities from Fortran modules, you can imagine a situation where variables or procedures with the same name could be imported from different modules. What happens in that case? Fortran will let you import two entities with the same name; however, it won’t let you reference them. The purpose of this design choice is twofold: First, it allows you to use just the use (without only) statement to import everything from modules, even if some entities may have conflicting names. Second, it doesn’t allow you to mistake one entity for another by accident.

What if you need a variable or function with the same name from different modules? Consider this scenario: You have a weather prediction model defined in mod_ atmosphere and an ocean prediction model defined in mod_ocean. Your job is to make them talk to each other as they simulate weather and ocean circulation. There’s just one problem: both mod_atmosphere and mod_ocean define an array called temperature. Having both arrays in your interface is critical for coupling the two models, and yet, you won’t be able to reference these arrays because of the name conflict.

As a workaround, Fortran allows you to rename the entity at the time of import:

use mod_atmosphere, only: temperature                     ❶
use mod_ocean, only: temperature_ocean => temperature     ❷

❶ Imports temperature as is

❷ Imports temperature as temperature_ocean

The key element here is the => operator on the second line. It allows you to import a variable or procedure under a different name. The first line in the snippet is analogous to Python’s from numpy import sqrt. The second line is analogous to Python’s from numpy import sqrt as np_sqrt. The operator => means “points to” rather than “is renamed as.” The name on the left side is the new name we want to use, whereas the one on the right side is the original name as defined in the module. (This may not be intuitive at first.)

If you need to rename multiple entities, separate them with a comma:

use mod_ocean, only: temperature_ocean => temperature, &
                     velocity_ocean => velocity

In summary, the easiest way to access a module is to import everything from it. use ..., only: ... lets you list explicitly the variables that you want to import. Finally, to resolve any name conflicts, you can use the => operator to rename the imported variables or procedures. Keep in mind that unlike Python, Fortran doesn’t have namespaces. As your application and library grow in size, and you import all entities from all modules implicitly, it becomes difficult to keep track of what came from what module, and there’s no way to find out until you look inside the modules for the declarations.

Let’s return to our finite difference functions in the mod_diff module. To avoid naming conflicts between the two functions, let’s call the old function diff_upwind and add the new function diff_centered. We can then import the centered difference function as diff in the main program:

use mod_diff, only: diff => diff_centered

At this point, you should have a mod_diff module that defines both the diff_upwind and diff_centered functions.

Renaming imported entities to avoid name conflict

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top