简体   繁体   English

有什么方法可以获取 Fortran 变量的名称?

[英]There any way to get name of Fortran variable?

I like how it's implemented in Python.我喜欢它在 Python 中的实现方式。 Example(Python):示例(Python):

x = 1
y = 2
print(f"{x = }, {y = }")
# x = 1, y = 2

Im want to handle the errors and then print the variable name.我想处理错误,然后打印变量名。 Example(Fortran):示例(Fortran):

function check(var)
...
    if (var < 0) print *, 'Error: var < 0'
...
end function check

There is no way in Fortran to get a variable name dynamically as variable names and types are required at compile time. Fortran 无法动态获取变量名,因为在编译时需要变量名和类型。

You could used Fortran derived types to associate a label to a value:您可以使用 Fortran 派生类型将 label 关联到一个值:

program derived_types
! Define
type :: labelled_int_var
  character(len=15) :: label
  integer :: value
end type

! Declare
type(labelled_int_var) :: x,y

! Initialize
x%value = 1
x%label = 'x'

y%value = 2
y%label = 'y'

! Use
write(*,*) trim(x%label), " =", x%value, ", ", trim(y%label), " =", y%value

end program

For Intel and GNU fortran compilers (by question tags), as well as for many other compilers, you can use a preprocessor ( -cpp flag).对于 Intel 和 GNU fortran 编译器(按问题标签)以及许多其他编译器,您可以使用预处理器( -cpp标志)。

#if defined(__GFORTRAN__) || defined(NAGFOR)
#   define CPPSTR(a) "a"
#else
#   define CPPSTR(a) #a
#endif

#define assert(cond) if(.not. (cond)) print *, "Assert fail: ", CPPSTR(cond)

#define printv1(v1) print *, CPPSTR(v1), (v1)
#define printv2(v1,v2) print *, CPPSTR(v1), (v1), CPPSTR(v2), (v2)
#define printv3(v1,v2,v3) print *, CPPSTR(v1), (v1), CPPSTR(v2), (v2), CPPSTR(v3), (v3)
#define printarray(a) print *, CPPSTR(a); print *, (a)

program t
    implicit none

    integer i, j

    i = 1
    j = 3
    assert(i .lt. 0)
    assert(i .gt. 0)

    printv3(i, j, i+j)
    printv1(([i, j, i+j]))
end program t

Output: Output:

 Assert fail: i .lt. 0
 i           1  j           3  i+j           4
 ([i, j, i+j])           1           3           4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以在不指定名称的情况下在 boto3 中获取当前 lambda function 的名称? - Any way to get name of current lambda function within boto3 without specifying the name? 有什么方法可以在 UI 中命名 Vertex Pipelines 组件 - Is there any way to name Vertex Pipelines components in the UI 有什么方法可以禁用或增加 Kubernetes 中的端口名称长度? - Is there any way to disable or increase port name length in Kubernetes? 如何获取 SQL 中变量的名称? - How can I get the name of a variable in SQL? 有没有办法将公司信息添加到 Alexa 服务并在没有调用名称的情况下使用它? - Is there any way to add company info to Alexa service and use it without invocation name? 有什么方法可以将目录名而不是单个文件名作为 AWS Lambda 测试事件中的键? - Is there any way to put a directory name instead of a single file name as a key in AWS Lambda test event? 有什么办法可以在 s3 中获取 awsglue 库? - Is there any way I can get awsglue libs in s3? Amazon Lex V2 Lambda python 用于获取意图名称或从 lambda 返回的任何响应的代码不起作用 - Amazon Lex V2 Lambda python code to get intent name or any response back from lambda is not working 有没有办法在谷歌云计算引擎上使用旧网站的域名? - Is there any way to use domain name of old website with project on Google Cloud Compute Engine? 有什么方法可以跟踪多个谷歌存储桶中的文件信息(大小、名称、存储桶位置、创建/更新时间戳) - Is there any way to track file information (size,name,bucket location,create/update timestamp) in multiple google buckets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM