Have a block import only some entities from host?

I am starting to use block in my codes and like the ability to declare variables close to their use. A block has access to variables in the host procedure. I wish I could write something like

program main
! some code
block, import only: x,y
! some code
end block
end program main

so that I could import only some entities from the host. Does Fortran already have this functionality, and if not, has it been considered?

You do it in F2018 like this:

program main
  implicit none
  integer :: x, y, z
  block
    import, only: x, y
    print *,x  ! valid
    print *,z  ! not valid due to import
  end block
end program main

ifort accepts the import statement but gfortran does not (per the bug linked by @kargl).

1 Like

The standards committee gets many arrows, but often I find that they anticipated my needs. Thanks to you and them.

Until gfortran implements this, I guess the workaround is to compile with ifort using import, only, and once that works, comment out the import, only so that the code works with gfortran as well.

Do you think you could push that patch at some point to be merged to trunk? One of the things that has stopped be from properly implementing import host associations in fortls was the fact that gfortran did not support them. It would be awesome if the patch got included with the next release.

Unfortunately, I understand very well where you are coming from. It is truly unfortunate that gfortran is not better funded. Judging from the results in the Fortran Developer's Poll - 2022 gfortran is almost universally used by all the participants at some point in the development/deployment cycle.

I am certain that there exist financially stable and profitable companies/institutions that use gfortran but have not considered donating the the gcc project.

1 Like