Roland Schilling wrote:
...
> Is there a way in Fortran95/03 to check whether a certain
> variable exists (is declared) without aborting the program
> if this is not the case?
>
> Such a variable to be tested for could be in a module, e.g.
> of foreign origin. Or it could be in a user's main program,
> and its existence should be checked for in a module USEd by
> this program.
>
> If there is no such possibility, wouldn't it be a good thing
> to extend the functionality of PRESENT for such a kind of
> test?
It depends on what you mean by "without aborting the program".
If you have specified IMPLICIT NONE, then references to an
undeclared variable should generate an error at compile time.
If you want to guarantee that a specific variable's declaration
comes from a specific module you should have an ONLY clause
on your USE:
USE my_mod, only: X
This should generate a error if there is no X in MY_MOD (this
is one of the things that the processor is *required* to detect and
report). So, even if there is some X declared elsewhere this will
tell you whether there's an X in MY_MOD.
Now, if you want even the compile to succeed without the variable
being declared, there's no way to do it. The PRESENT intrinsic
function can only be applied to variables that *are* declared (they
must have the OPTIONAL attribute). I suppose it might be possible
to extend the concept of OPTIONAL to module entities. A plausible
new feature might be something like:
USE my_mod, optional: X
And then allow PRESENT to be applied to X just as if it were an
optional dummy argument. I doubt that you could get such a feature
through the committee.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
|