I'm looking through some old Fortran 90 code and have come across the =>
symbol:
var => item
It looks like it's being used for some sort of assignment.
Searching Google for "arrow symbol Fortran" or "equals greater than symbol Fortran" gives me no related material.
=>
appears in six contexts as a syntactic element in modern Fortran, many, but not all, related to pointers: pointer assignment; pointer initialization; procedure (pointer) declaration; type-bound procedure declaration; association; renaming. There is close connection between most of these. Loosely, in many =>
can be viewed as providing an alternative temporary or permanent means of referencing another entity. In none, however, is =>
acting as an operator.1
Pointer assignment is one of the traditional appearances of =>
, appearing in Fortran 90. It is used to associate a pointer with a target, and is explained in another answer.
Renaming of entities use associated involves the element =>
and is the other appearance in Fortran 90/95 code. Such use
statements are like
use mod_a, c=>a
use mod_b, only : cc => bb
Following such a use
statement the module entities a
and bb
are known by the local names c
and cc
.
Pointer initialization is much like pointer assignment where =>
appears. Pointer initialization defines the initial pointer association of the pointer. We see this in statements such as
real, target :: a
real, pointer :: b=>a
real, pointer :: c=>NULL()
With this explicit initializtion, the pointer b
here is initially associated with a
and the pointer c
is initially unassociated. These two forms are helpful in modern Fortran in that in Fortran 90 a pointer always starts life initially of undefined association status. Here we know that b
and c
have defined pointer association status, and we can use ASSOCIATED
.
For derived type components such syntax gives default initialization.
Much as with explicit/default initialization for data objects =>
features in defining the initial association of a procedure pointer.
procedure(proc_interface), pointer :: proc1 => donkey
procedure(proc_interface), pointer :: proc2 => null()
Here, proc1
is again initially associated with the procedure donkey
and we can have such things as
call proc1(...)
much as we could when proc1
is pointer assigned with donkey
outside the declaration statement. proc2
is, as expected, initially not associated.
As with data object components of derived types =>
can feature in setting the initial association of a derived type's procedure pointer component:
type a
procedure(proc_interface), pointer :: proc => donkey
end type a
Conceptually related to the above is the use of =>
in declaring type-bound procedures.
type a
contains
procedure :: proc => donkey
generic :: assignment(=) => type_a_eq
end type a
Here in the type a
proc
is a binding name so that b%proc
(for b
an entity of type a
) is a procedure reference. We also, through type_a_eq
, have defined assignment with entities of type a
on the left-hand side.
=>
appears in four association contexts. The associate
, change team
, select type
and select rank
constructs associate a name with a selector.
From Fortran 2003 we have
associate (a => 1+2)
... ! In this block we have an entity named a
end associate
and
class(*) b ! b is unlimited polymorphic
select type (a => b)
type is (real)
... ! a is a non-polymorphic real here
end select
Introduced in Fortran 2018 we have
dimension(..) b ! b is assumed rank
select rank (a => b)
rank (1)
... ! a is a rank-1 array here
end select
and
change team (team_value, a[*] => b)
... ! In this block we have a coarray named a
end team
These associations differ from pointers. a
in the associate
block needn't be (and in the example above isn't) a variable.
[1] The concept of operator is precisely defined in the Fortran language, such as in 3.2.4 of the Fortran 2008 specification. Unlike many other languages, assignment (with =
) itself is not an operation, but a statement. In C-like languages we can have (a=b)
as an expression returning a result: this is not the case in Fortran. The same holds for pointer assignment in Fortran. The other cases above are wholly distinct from this idea of assignment. In Fortran, even =
appearing in initialization is not the same thing as assignment. =>
cannot be viewed as having one effect.
Surprisingly, searching "equals arrow symbol Fortran" yields some results.
"=>" is commonly referred to as the pointer assignment operator.
(Though it is not truly an operator as it returns no value.)
It is used to associate a chosen reference name with a target:
reference => target
The above can be read as "reference refers to target"
Most often this reference occurs in the form of a pointer. In this case you could say "reference points to target". This is a helpful way to remember what this operator does as its literal appearance is of an arrow pointing from reference to target.
Further Uses
Additional uses including making local aliases for various items such as module components, procedures, and even arbitrary expressions. For a full explanation of all of these uses see this answer.
Pointer assignment vs. Traditional Assignment (=)
Here's a quick example from the above link that illustrates how pointer assignment differs from classic assignment ("="). Basically this shows that once a target has been established, the pointer is treated as that target for basic statements.
pt => x ! pt points to x
y = pt ! y equals x
pt => y ! pt points to y
pt = 17 ! y equals 17
Other Resources:
General documentation on Fortran pointers
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