In python a statement like this:
from module import function354
makes sense, because Python is an interpreter language and I don't want python to load all 353 other functions.
Fortran has a similar construct:
use module, only : function354
Why would I use this? The compiler creates a *.mod file anyway, compiling all function. Is there any performance advantage (in compile or run time) if I specify an only
-statement?
I can see that it might be helpful to avoid naming conflict, but other than that I don't really see the point.
Any declared entities in a module that are intended to be used externally can be accessed by Fortran 90's 'USE' statement. The 'USE' statement can be included in any program unit, function or subroutine of a Fortran 90 code. Its syntax can be described in two forms.
If a statement function is referenced, the defined calculations are inserted. The statement function argument list indicates the order, number, and type of arguments for the statement function. A statement function is referenced by using its name, along with its arguments, as an operand in an expression.
Modules are containers for variables and subprograms. Modules are useful for creating global variables and grouping related subprograms. Modules consist of variables, data type definitions, subroutines, and functions.
An abstract interface allows you to specify procedure characteristics and dummy argument names without declaring a procedure with those characteristics.
Two main reasons
use, only
is a useful addition to aid thisuse, only
is a nice feature to aid code readabilityYou don't just want fast code - as important is maintainability, and more important is correctness!
Some people has pointed (correctly) in the comments that use rename => name
can be used independently of use, only
. Like this:
use module, f354 => function354
I am keeping the answer because it could be useful as complementary information on the use
statement for someone that lands here.
Just for completeness, there is another feature that use, only
provides, that is the ability to rename imported names with locally-bound names (only is not needed for this, see disclaimer above).
Like this:
use module, only: f354 => function354
That has proven useful for me in several different scenarios:
Resolve specific name ambiguities when two used modules provide types or functions with the same name.
Example:
use module1, only: m1_init => initialize
use module2, only: m2_init => initialize
Use short names when the original name is too long, too cryptic or when it is used very often in your program.
Example (from fgsl and lapack):
use fgsl, only: stride => fgsl_aux_vector_double_stride ! too long
use lapack, only: solve => dgesvx ! too cryptic
use iso_c_binding, only: i32 => c_int32_t, i64 => c_int64_t ! too frequent
Make it easier to refactor or use conditional compilation / templating:
Example (from Fortran-lang/stdlib):
use iso_fortran_env, only: sp => real32, dp => real64, qp => real128
! If we decide later to use iso_c_binding instead of iso_fortran_env:
! use iso_c_binding, only: sp => c_float, dp => c_double, qp => c_float128
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With