Modules at Scale
Only Import
Keeping Scope Small
use ..., only: imports selected public names from a module instead of bringing every public name into scope.
Program
Play the program to choose a factor and call only the imported scaling function.
only_import.f90
Replay: real traced execution (multi-file project)
module scale_tools
implicit none
private
public :: scale
contains
function scale(value, factor) result(total)
integer, intent(in) :: value
integer, intent(in) :: factor
integer :: total
total = value * factor
end function scale
end module scale_tools
program only_import_demo
use scale_tools, only: scale
implicit none
integer :: factor
integer :: total
factor = 4
total = scale(10, factor)
print '(I0)', total
end program only_import_demo
module scale_tools
implicit none
private
public :: scale
contains
function scale(value, factor) result(total)
integer, intent(in) :: value
integer, intent(in) :: factor
integer :: total
total = value * factor
end function scale
end module scale_tools
program only_import_demo
use scale_tools, only: scale
implicit none
integer :: factor
integer :: total
factor = 2
total = scale(10, factor)
print '(I0)', total
end program only_import_demo
module scale_tools
implicit none
private
public :: scale
contains
function scale(value, factor) result(total)
integer, intent(in) :: value
integer, intent(in) :: factor
integer :: total
total = value * factor
end function scale
end module scale_tools
program only_import_demo
use scale_tools, only: scale
implicit none
integer :: factor
integer :: total
factor = 5
total = scale(10, factor)
print '(I0)', total
end program only_import_demo
factor ← 4
20factor = 421total = scale(10, factor)values this step4factortotal ← 40
20factor = 421total = scale(10, factor)22print '(I0)', totalvalues this step40total4factorprint '(I0)', total
21 total = scale(10, factor)22 print '(I0)', total23end program only_import_demooutput40values this step40total
factor ← 2
20factor = 221total = scale(10, factor)values this step2factortotal ← 20
20factor = 221total = scale(10, factor)22print '(I0)', totalvalues this step20total2factorprint '(I0)', total
21 total = scale(10, factor)22 print '(I0)', total23end program only_import_demooutput20values this step20total
factor ← 5
20factor = 521total = scale(10, factor)values this step5factortotal ← 50
20factor = 521total = scale(10, factor)22print '(I0)', totalvalues this step50total5factorprint '(I0)', total
21 total = scale(10, factor)22 print '(I0)', total23end program only_import_demooutput50values this step50total
only
`only: scale` imports just the names the program needs.
private default
`private` makes the module surface explicit.
public list
`public :: scale` documents the exported API.