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?

See 22-007r1.pdf. It answers your questions. Whether any compiler works is another story.

Edit: See Fortran 2018. It answers the question as well.

Edit**2: 106035 – F2018 allows an IMPORT statement within the BLOCK construct.

Edit**3: A patch has been attached to the bugzilla report. It is sufficient that for most users gfortran will do what you want. I no longer commit patches to the actually GCC source code repository. So, if someone wants to champion a patch and get involved with gfortran developement, here’s your opportunity.

You do it in F2018 like this:

program main
  implicit none
  integer :: x, y, z
    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.

I did implement the F2018 feature. The patch attached to the bug report has been superseded by one in my local source code repository. import none and import all work. import, only : i works with a caveat. The caveat is that how import was originally implemented needs to be ripped out and redone. I doubt I’ll submit a new patch. A correct fix would require someone sponsoring the work. import none is the only case of interest to me.

1 Like

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.

At this point in time, I will not be submitting the patch. Checking the constraints, import, none, and import, all appear to work. Unfortunately, import, only : works for a very limited situation, which renders this usage as unusable.

This is an example, where J3 made a change of a feature in the language, that looks great from a user viewpoint. It unfortunately has the side effect that a vendor may need to redesign how import was implemented to meet F2003 and F2008 requirements. This is fine for a commercial vendor, who has a financial incentive to fix its compiler. For a project, which is primarily supported by volunteers, it becomes a burden.

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.