Discussion:
Call subroutine at first "USE" of a module?
(too old to reply)
s***@gmail.com
2016-11-28 16:04:37 UTC
Permalink
Does Fortran 2008 provide a method to automatically call a module subroutine at the first "USE" of a module?

Thanks...
Stefano Zaghi
2016-11-28 16:14:28 UTC
Permalink
Post by s***@gmail.com
Does Fortran 2008 provide a method to automatically call a module subroutine at the first "USE" of a module?
Thanks...
Dear sgih, to my knowledge no, I am not aware of such a feature. Anyhow, it is likely possible that I am wrong (indeed, I hope so, it is a very useful feature that I exploited in Python): maybe, there are some tricks... Let us read other answers.

My best regards.
Thomas Jahns
2016-11-28 16:43:46 UTC
Permalink
Post by s***@gmail.com
Does 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.

This is probably an artifact of Fortran being a compiled language where name
resolution happens at another stage than execution.

Regards, Thomas
h***@gmail.com
2016-11-29 00:38:07 UTC
Permalink
Post by s***@gmail.com
Post by s***@gmail.com
Does 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.com
This 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.
Thomas Jahns
2016-11-29 10:17:58 UTC
Permalink
Post by h***@gmail.com
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.
That gives rise to the C++ problem of having static object variable and constant
initializers running in a basically undefined order before main. That is a known
head-ache:

[10.12] What's the "static initialization order fiasco"?
http://yosefk.com/c++fqa/ctors.html#fqa-10.12

I hope Fortran can learn from this exercise. I'm not sure this works any better
in Java than C++.

Thomas

Wolfgang Kilian
2016-11-28 16:50:08 UTC
Permalink
Post by s***@gmail.com
Does Fortran 2008 provide a method to automatically call a module subroutine at the first "USE" of a module?
Thanks...
No.

USE is not an executable statement. In particular, there is no order of
execution for modules, such a concept does not exist.

The language has become more liberal about what can be called for
initializing a variable, but to my knowledge this does not include
user-defined procedures. (Initialization happens at an undefined time
before program execution begins.) So you must determine the first use of
an entity yourself.

-- Wolfgang
Loading...