Post by s***@gmail.comPost by s***@gmail.comDoes Fortran 2008 provide a method to automatically call a
module subroutine at the first "USE" of a module?
USE only makes symbols/names available ("association") it's no executable
statement and thus has no defined position in the sequence of instructions that
make up a Fortran program or sub-program.
Yes, the question written that way doesn't apply.
But one could have routines that are automatically called at
the beginning of program execution, before the beginning of MAIN.
Java has static initializers that are called at class load time.
https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
In the usual case, Java programs don't have to know about
when class load is, just that it is before the first use of the class.
Mostly, it allows for initialization of static variables with more
complicated initialization that can be done the usual way.
(I suspect Fortran initializers can already do more than Java
initializers.)
But anyway, the idea would be to initialize module variables before
any other reference to those variables. If referenced through a
module subroutine, you can test a SAVEd variable to do the
initializing once, the first time it is called. But for module variables
referenced directly, you can't do that.
The Java syntax is the same as a static method, but without
a method name and without arguments, just the word static.
Seems to me that the Fortran equivalent would be:
SUBROUTINE
with no name and no (well, they are already optional) arguments.
Post by s***@gmail.comThis is probably an artifact of Fortran being a compiled language
where name resolution happens at another stage than execution.
Java is a compiled language, even if the compiler output is most
often interpreted.
In Fortran, it could be done either by the initialization done
by MAIN (which might now need to initialize some I/O system),
or by routines that USE the module, (which could be MAIN), but
only the first time. It could be done, for example, at the top of
any routine that used a variable from the module, with a test of
a static variable to do it only once.
As this seems to be needed by OO languages, it might be that
it goes along with OO Fortran.