有没有办法使用字符串调用派生类型的字段? [英] Is there a way to call the field of a derived type using a string?

查看:172
本文介绍了有没有办法使用字符串调用派生类型的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



类似于...


$有没有办法在fortran中通过字符串参数调用派生类型的字段? b $ b

 子程序集(car,fieldName,value)
类型(Car_T):: car
字符*(*):: fieldName
字符*(*)::值

car%[fieldName] =值
结束子程序集

我知道你可以在javascript,c#等等中做这样的事情,但是如果fortran允许的话,这可以让我有大量重复的代码。

解决方案

您可以使用名称列表做类似的事情,但它是针对程序已知的项目:不是新项目。

  integer :: inin 
real :: rere
namelist / info / inin,rere

inin = 0!默认
rere = 20.4!默认
读取(*,nml = info)
print *,'inin =',inin
print *,'rere =',rere

stop
结束

在输入

 & info inin = 2 rere = 40.0 / 

您只希望输入一个值

 & info rere = 3.162 / 


Is there a way to call the field of a derived type via string argument in fortran?

something like...

subroutine set(car, fieldName, value)
    type(Car_T) :: car
    character*(*) :: fieldName
    character*(*) :: value

    car%[fieldName] = value
end subroutine set

I know you can do stuff like this in javascript, c#, ect., but this could really help me from having a ton of duplicate code if fortran allows it.

解决方案

You can do something similar with namelists but it is for items known to the program: not new items.

integer:: inin
real:: rere
namelist /info/ inin, rere

inin = 0 ! default
rere = 20.4 ! default
read (*, nml=info)
print *, 'inin=', inin
print *, 'rere=', rere

stop
end

On the input

&info inin=2 rere=40.0 /

Or if you wish to input one value only

&info rere=3.162 /

这篇关于有没有办法使用字符串调用派生类型的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆