Shared data and illegal statements in MODULE

I’m sure the answer to this is something simple to do with the Fortran standard, but for the life of me I can’t figure out what.

Namely, I’m trying to make up a module that contains a whole bunch of definitions of arrays and initialize their values. So, I tried and got a failure. A simple program that demonstrates the way I thought of doing this is:

module tinyconstants

implicit none

real, dimension(5)       :: hk_uv_const
hk_uv_const=[1.0, 2.0, 3.0, 4.0, 5.0]

end module tinyconstants

When I try to compile this:

> pgfortran -c tinyconstfail.f90 
PGF90-S-0310-Illegal statement in the specification part of a MODULE  (tinyconstfail.f90: 6)
  0 inform,   0 warnings,   1 severes, 0 fatal for tinyconstants

But, if I try a different method:

module tinyconstants

implicit none

real, dimension(5) :: hk_uv_const=[1.0, 2.0, 3.0, 4.0, 5.0]

end module tinyconstants

and compile:

> pgfortran -c tinyconstwork.f90

No errors!

It’s not exactly hard to use the second style, but why is the first example a violation of the standard?

Hi Matt,

Only definitions are allowed in the in module and the data initialization statement is considered part of the definition. The assignment operation is an executable statement hence is not allowed.

One way to look at it, is with the data initialization, the compiler will hard code the values in the object’s static data section. When the object is loaded, the values are already populated.

  • Mat

That sounds perfectly reasonable. I guess I’ve only put small arrays or constants in modules like this before (so it made sense to initialize and declare at same time). This time I had 2, 3D arrays with reshapes and I guess my mind thinks in execution format.

I also learned that you can’t use TRANSPOSE in an initialization statement. That was more obvious as to why you couldn’t.