On 11/4/19 8:31 PM, Beliavsky wrote:
> On Monday, November 4, 2019 at 9:18:23 PM UTC-5, Ron Shepard wrote:
>> On 11/4/19 11:27 AM,
pkla...@nvidia.com wrote:
>>> You can rename entities that are USE-associated, e.g.
>>>
>>> use foo, only :: foo_bar => bar
>>
>> As far as I know, however, it is not possible to rename the module
>> itself. This prevents a programmer from using two different module
>> libraries that happen to be named the same thing in the same program
>> unit. That seems like it might be a nice thing to do sometimes, but I
>> don't really see a good way to implement that functionality.
After I posted this, I realized that it is possible after all. The
programmer could define a new module
module foo2
use foo
end module foo2
This basically renames the module from foo to foo2, and it allows both
foo2 and the second foo module to be used in the same program unit. Of
course, when this module is compiled, it would need to identify the
"correct" foo, and then later when the "other" foo is accessed, the
compiler would need to correctly identify that, but as far as the
fortran part itself is concerned, this would at least be a workaround
for the missing rename functionality.
>>
>> As for the original question, there are many disadvantages to the idea
>> of prepending the module name to references of the the entities within
>> it. The most obvious is that changes within the implementation of the
>> module might then require changes in the calling program. That pretty
>> much negates many of the advantages of modules, and of libraries in
>> general, along with the general idea of abstraction.
>
> If I could write
>
> y = foo%bar(x)
>
> the implementation of bar in module foo could change without having to change the calling code.
What if bar() within foo was originally written as a contained
procedure, in which case it would be referenced as above. Then later, it
is decided to refactor the routines and bar() is instead moved into
another module, say "new", which is used by foo. Now the references
would all need to be changed to
y = foo%new%bar(x)
As I said above, this notion is contrary to the idea of abstraction and
libraries. It requires the calling program to know the details of the
implementation of the whole library module hierarchy.
There is only a problem if bar is moved to a module other than foo, but
in my code I currently write
>
> use foo, only: bar
> y = bar(x)
>
> so my code would need to change in any case. Just writing
>
> use foo
>
> is something I avoid because I want to know where imported procedures and variables are coming from.
I do the same thing, I like to know where things come from too, but only
one level at a time. In your example, if bar() is implemented within
module new, then module foo will have something like
module foo
use new, only: bar ! plus whatever else is imported
...
end module foo
Now when module foo is USEd, it is not necessary to know exactly how
bar() was implemented, those details are abstracted, and the calling
program need not be changed if/when those details change in the future.
$.02 -Ron Shepard