ASSOCIATE(7f) - [FORTRAN:EXECUTION CONTROL] associate construct (LICENSE:PD)
[ associate-construct-name : ] ASSOCIATE ( associate-name => expr|variable, ...)
Basically, ASSOCIATE allows you toIt is _NOT_ equivalent to a function macro.
1. create an alias for a variable 2. evaluate an expression at the top of the block and use a variable name as a substitute for the constant, much like a parameter declaration. The ASSOCIATE construct associates named entities with expressions or variables during the execution of its block. These "named construct entities" (16.4) are "associating entities" (16.5.1.6). The names are "associate names".
o If selector is not a variable or is a variable that has a vector subscript, associate-name shall not appear in a variable definition context(16.6.7). o An associate-name shall not be the same as another associate-name in the same associate-stmt. o variable shall not be a coindexed object. o expr shall not be a variable. o end-associate-stmt is END ASSOCIATE [ associate-construct-name ] o (R806) If the associate-stmt of an associate-construct specifies an associate-construct-name, the corresponding end-associate-stmt shall specify the same associate-construct-name. If the associate-stmt of an associate-construct does not specify an associate-construct-name, the corresponding end-associate-stmt shall not specify an associate-construct-name.
Execution of an ASSOCIATE construct causes evaluation of every expression within every selector that is a variable designator and evaluation of every other selector , followed by execution of its block. During execution of that block each associate name identifies an entity which is associated (16.5.1.6) with the corresponding selector. The associating entity assumes the declared type and type parameters of the selector. If and only if the selector is polymorphic, the associating entity is polymorphic.
It is permissible to branch to an end-associate-stmt only from within its ASSOCIATE construct.
Within an ASSOCIATE or SELECT TYPE construct, each associating entity has the same rank and corank as its associated selector. The lower bound of each dimension is the result of the intrinsic function LBOUND (13.7.90) applied to the corresponding dimension of selector . The upper bound of each dimension is one less than the sum of the lower bound and the extent. The cobounds of each codimension of the associating entity are the same as those of the selector. The associating entity has the ASYNCHRONOUS or VOLATILE attribute if and only if the selector is a variable and has the attribute. The associating entity has the TARGET attribute if and only if the selector is a variable and has either the TARGET or POINTER attribute. If the associating entity is polymorphic, it assumes the dynamic type and type parameter values of the selector. If the selector has the OPTIONAL attribute, it shall be present. The associating entity is contiguous if and only if the selector is contiguous.
If the selector is not permitted to appear in a variable definition context (16.6.7), the associate name shall not appear in a variable definition context.
The following example illustrates an association with an expression.
associate ( z => exp(-(x**2+y**2)) * cos(theta) ) print *, a+z, a-z end associateThe following example illustrates an association with a derived-type variable.
associate ( xc => ax%b(i,j)%c ) xc%dv = xc%dv + product(xc%ev(1:n)) end associateThe following example illustrates association with an array section.
associate ( array => ax%b(i,:)%c ) array(n)%ev = array(n-1)%ev end associateThe following example illustrates multiple associations.
associate ( w => result(i,j)%w, zx => ax%b(i,j)%d, zy => ay%b(i,j)%d ) w = zx*x + zy*y end associate
The following example uses the ASSOCIATE construct as a shorthand for a complex expression and renames an existing variable, MYREAL. After the end of the ASSOCIATE construct, any change within the construct to the value of the associating entity that associates with MYREAL is reflected.See if you know what the following produces ...
program demo_associate real :: myreal, x, y, theta, a x = 0.42 y = 0.35 myreal = 9.1 theta = 1.5 a = 0.4 associate ( z => exp(-(x**2+y**2)) * cos(theta), v => myreal) print *, a+z, a-z, v v = v * 4.6 end associate print *, myreal end program demo_associate
program dustycorner implicit none real :: a = 42 associate (a => a, b => 2 * a) print *, a, b a = 0 print *, a, b end associate print *, aDid you expect the following?a=42 associate (aa => a, b => 2 * a) print *, a, aa, b aa = 0 print *, a, aa, b a=-1 print *, a, aa, b end associate print *, a end program dustycorner
42.0000000 84.0000000 0.00000000 84.0000000 0.00000000 42.0000000 42.0000000 84.0000000 0.00000000 0.00000000 84.0000000
-1.00000000 -1.00000000 84.0000000 -1.00000000
associate (7) | March 11, 2021 |