Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

pointers - What does => (equals greater than) mean in Fortran?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

=> 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

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.

Use association renaming

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

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.

Procedure declaration

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 an object-bound procedure

type a
  procedure(proc_interface), pointer :: proc => donkey
end type a

Type-bound procedure declaration

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.

Association

=> appears in three association contexts. The associate, 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 (the current draft of) 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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...