Re: Fortran has little support for object-oriented



Richard E Maine wrote:
Ben Hetland <ben.a.hetland@xxxxxxxxx> wrote:
Namespaces in C++ allows (a set of) names to
be isolated into their own "area", not conflicting with the same names
of other namespaces or the global namespace.

A module does not do this in Fortran. Names in a module become part of
the "global" namespace, and the same name in two different modules would
cause a conflict if both were USEd in a subroutine. The ONLY keyword
somewhat helps in this regard.

I'm no expert in C++, so I won't try to speak to comparisons to it, but
the above stqtements about modules are flat wrong.

Identifiers in modules are not global. The standard is quite explicit
about that. See the early parts of chapter 16. The version in f2003 is
cleaned up quite a bit, as earlier versions had some major infelicities
in the area of confusing entities and their names. But while those
infelicities affected other things, they didn't affect this one. Perhaps
you are confusing module names with the names of entities in the
modules. Modules names are global; the identifiers in the module are
not.

You're talking about global _scope_. Namespace, as C++ defines it, is a completely different thing from scope -- and Ben's comments are pretty much correct about namespace (with the caveat that RENAME doesn't fit into that concept very neatly at all). The fact that a name is "in the global namespace" does _not_ mean that it's a "global name" in the sense you're referring to.

Namespaces work like this in C++: any identifier has both a namespace and a scope. Scope determines whether you can reference the identifier (just as in Fortran); namespace determines what you call it when you do reference it. If the variable's namespace is in the current set of namespaces, you just use its name; if it's outside, you have to prefix it with the name of its namespace. Being in the global namespace just means that you never have to prefix it with the name of its namespace. It is entirely possible (and, in fact, common) to have names that are in the global namespace but are quite local in scope. One can even have multiple identical names in the global namespace if they never overlap in scope.

If Fortran had module-level namespaces akin to C++'s namespaces, the result might end up something like this:

First, suppose we have a module:

module modA
function my_fun(that)
...
end module

Then, if we use it in something else that has a different namespace (e.g., the main program), we have to explicitly give the namespace of my_fun when calling it. I'll use a ? as the syntax for that, just for the sake of argument:

program one
use modA
...
var2 = modA?my_fun(var1)
end program

Alternately, one could explicitly include modA's namespace within the current namespace and then use my_fun's name directly, as perhaps with the following syntax:

program one
use modA, namespace
...
var2 = my_fun(var1)
end program

The advantage of this is that, if one has two modules, there are never any name conflicts unless one explicitly combines the namespaces. For example:

module modA
function my_fun(that)
...
end module
module modB
function my_fun(that)
...
end module
program one
use modA
use modB
...
var2 = modA?my_fun(var1) + modB?myfun(var1)
end program

That's sort of the basics of it; there are, of course, notable complexities.

Fortran already addresses this problem with the renaming option on USE. The difference between the two approaches is largely that in C++ two identical names from different sources that have been bought into scope don't conflict unless you tell them to, whereas in Fortran they do unless you tell them not to.

- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.
.