首页 > 解决方案 > 在同一个 ASSOCIATE 构造中引用另一个关联实体中的关联名称

问题描述

如果我的标准语不是很好,我很抱歉,但是以下 Fortran 2008 不是合法的吗?

program test
  implicit none

  associate ( a => 6, b => 2*a )
    print*, b
  end associate

end program

我的编译器抱怨a没有被声明并且没有隐式类型。

编辑:

我认为选择器只能是表达式或变量,而不能是关联名称或涉及其中的表达式。是这样吗?

标签: fortran

解决方案


这是一个(嵌套)版本,可以满足您的需求,而您的版本不符合 Fortran 标准,

program test
implicit none
integer a ! note that a has be defined here because it is used as a VARIABLE in the association list below
associate ( a => 6, b => 2*a )
    print*, b
end associate
associate ( a => 6 )
    associate ( b => 2*a )
        print*, b
    end associate
end associate
end program

这是Gfortran编译的程序的输出,

$gfortran -std=gnu main.f90 -o main
$main
   483386368
          12

根据 Fortran 标准,该构造允许在块的持续时间内将associate名称与变量或表达式的值相关联。这是一般语法,

[ construct-name: ] associate ( association-list )
    block
end associate [ construct-name ]

因此,我认为您对该associate构造的使用不符合标准。基本上,在您的代码中,编译器假定关联列表a中的b => 2*ain 指的a是已在关联构造和列表之外定义的变量(与关联列表中定义的名称相反a)。

正如@HighPerformanceMark 所建议的那样,associate像上面的示例这样的嵌套结构可以实现您想要的目标。


推荐阅读