4

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
    The code snippet that you have is taken from the book "Modern Fortran Explained" by Metcalf et al. Can you explain your question in more detail? Yes, you can add `implicit none` and `import` statements inside your module procedure. But why do you need the `import` statement? – Scientist May 13 '21 at 05:59
  • Currently, I'm Learning Fortran! So I want to learn all the possible things in module procedures. Any simple example program which uses import statements is good enough! Thank you – Addiction99 May 13 '21 at 06:15
  • I've removed asking about implicit, because that's happily covered in [another question](https://stackoverflow.com/q/24337413/3157076). (Strictly, that asks about `implicit none`, but to answer that it's necessary to talk about things more generally.) – francescalus May 13 '21 at 07:46
  • 1
    Welcome, I suggest to take the [tour] and read [ask]. At first I misunderstood your question. You can certainly add `implicit` almost anywhere, but I do not see the reason for any `import` because you have host association. Or do you want to actually `use` some other module? – Vladimir F Героям слава May 13 '21 at 07:47
  • 1
    One of the things I'm most interested in in the most recent standards is use of import to control host association. However the question is not very clear, "doubt" is a strange word to use here, at least for a native UK English speaker - but I've seen similar especially in the chemistry group so it must be common somewhere. Does the OP just mean he gets some kind of error or warning at compilation time? In this case updating the compiler is the way to go? Or does it just mean "I am not sure if I can use ..." – Ian Bush May 13 '21 at 07:57
  • 1
    @francescalus But that book certainly did not consider Fortran 2018. But I must admit I still mostly ignore submodules due to insufficient compiler support (in sufficiently many preceding versions, not just in the current one) so if one actually uses `import` in F2008 submodules - well, I just missed that, I would not be *that* surprised about that. – Vladimir F Героям слава May 13 '21 at 08:05
  • @VladimirF the latest version of MR&C certainly does include F2018 - the subtitle is even "Incorporating Fortran 2018", and the above example is from section 16.2 – Ian Bush May 13 '21 at 08:25
  • @IanBush OK, since they stopped putting the version into the title, it is unclear which edition the other person refers to. My edition of MFE precedes year 2018 but does already include submodules. Maybe I should order another edition for our library. – Vladimir F Героям слава May 13 '21 at 08:31

1 Answers1

6

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.

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • I think it's gfortran-10 you need for the F2018 support – Ian Bush May 13 '21 at 08:26
  • @IanBush, 10.2 is the latest I've tried (instead of reading the docs), but that fails with "Error: IMPORT statement at (1) only permitted in an INTERFACE body". I think Intel have supported for a while. – francescalus May 13 '21 at 08:28
  • OK, looks like you are right. I could have sworn I found it in gfortran but can't reproduce that now. Pity. – Ian Bush May 13 '21 at 08:36
  • Not in 11.1 either. But then, it's not just about "what compiler am I using?", but "what do my code analysis tools support?" and "what compilers are my customers using?". Unfortunate indeed, because host association control is a good tool for code quality. – francescalus May 13 '21 at 08:43
  • "host association control is a good tool for code quality" Couldn't agree more – Ian Bush May 13 '21 at 08:50