USE with ONLY

Import only names

Plain use imports everything, which clutters the namespace: the only clause lists exactly the names this program needs.

With use stats, only double_it the program sees one procedure and nothing else, so stray names cannot collide.

program onlydemo use stats, only: double_it implicit none print *, double_it(21) end program onlydemo

gfortran stats.f90 main.f90 -o onlydemo && ./onlydemo

42

Rename on import

Rename imports to fit local style or dodge a clash: twice becomes a local alias for double_it with no copy and no wrapper.

The new name points at the same procedure, so every fix in the module reaches the alias at once.

program renamedemo use stats, only: twice => double_it implicit none print *, twice(21) end program renamedemo

gfortran stats.f90 main.f90 -o renamedemo && ./renamedemo

42

Avoid clashes

Two modules may export the same name: importing both plainly is ambiguous, but two narrow only lists with a rename keep them apart.

Both modules below export run. Pick step from physics and draw from graphics, and each call resolves to exactly one procedure.

module physics implicit none contains subroutine run() implicit none print *, "step" end subroutine run end module physics

module graphics implicit none contains subroutine run() implicit none print *, "draw" end subroutine run end module graphics

program clash use physics, only: step => run use graphics, only: draw => run implicit none call step() call draw() end program clash

gfortran physics.f90 graphics.f90 main.f90 -o clash && ./clash

step
draw

One use per need

Keep each use line small and local: import at the top of the unit that needs it and list only the names that unit calls.

Small import lists document dependencies at a glance and make future renames painless.

program styled use stats, only: double_it use physics, only: step => run implicit none print *, double_it(3) call step() end program styled

gfortran stats.f90 physics.f90 main.f90 -o styled && ./styled

6
step

The code on this page was run with gfortran 15.2.0 on Alpine Linux 3.24 (x86_64) in a Docker container.

Article author: Arthur Isaev