David Zhang
David Zhang

Reputation: 4364

How do I specify runtime constants in (modern) Fortran?

I'm working on a Fortran 2008 project in which I read some parameters (including both scalars and arrays) from an input file using read statements. I would like to enforce that these parameters not be modified after they are read in. How can I achieve this?

Upvotes: 3

Views: 255

Answers (2)

IanH
IanH

Reputation: 21431

Module variables can be given the PROTECTED attribute, which prevents code USE'ing the relevant module from modifying the module variable through the name of the variable.

However this does not prevent modifications to the variable from within the defining module or its descendant submodules, or modifications to the variable through means other than the use associated name (e.g. through a pointer associated with the same thing), or stop non-conforming Fortran code from modifying the variable (e.g. by the use associated name being used as an actual argument to a procedure with implicit interface that modifies the corresponding dummy).

Upvotes: 4

innoSPG
innoSPG

Reputation: 4656

In fortran 90, You can define a module that has those variables as private member. Let the module define only the function to read your variables from file, and getter functions that return their values. And no other function in the module.

I do not use the OO capability offer by fortran 2003 an fortran 2008, but the scenario will be similar.

Upvotes: 1

Related Questions