IMPORT statement within module procedures

Viewed 297

Currently I'm learning Fortran. I came across procedures

submodule (points) points_a
contains
  module procedure point_dist
    distance = sqrt((a%x - b%x)**2 + (a%y - b%y)**2)
  end procedure point_dist
end submodule points_a

I am not sure if I can use an import statement inside the module procedure block? Can anyone share an example program?

1 Answers

When it comes to the IMPORT statement, you have to ask yourself a question: am I using Fortran 2008 or Fortran 2018? Fortran 2018 significantly expanded this statement.

In Fortran 2008 the IMPORT statement makes available within an interface body entities that are defined outside that interface body (from the host scoping unit). In the question example there is no interface body and so a Fortran 2008 IMPORT statement is not allowed.

The Fortran 2008 form of the IMPORT statement is the form most commonly supported by compilers and tools.

Fortran 2018 allows the IMPORT statement to more fully control host association. We have the statements which look like

import
import host_entity

which are the same as in Fortran 2008. But we also have

import, only : host_entity
import, none
import, all

With these statements, we say what entities are host associated within the scoping block. import, only : ... gives a list of those host entities which are available. import, none says no entities from the host are accessible through host association (but may be by other means); import, all which makes all host entities accessible through host association (and these cannot be "shadowed" locally).

The Fortran 2018 IMPORT statement can be used in any scoping unit which has a host scoping unit. (This includes module procedures within a submodule, as in the question.)

At the moment, you are likely to see your compiler/tool telling you that IMPORT can appear only in interface bodies. Again, this is down to language level support.

Related