一个Fortran函数/子例程,可以返回实数,整数或字符串. [英] A Fortran function/subroutine that could return either a real, an integer or a string.

查看:277
本文介绍了一个Fortran函数/子例程,可以返回实数,整数或字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个返回实数,整数或字符串的函数.

I would like to know how to create a function that either returns a real, an integer or a string.

例如,呼叫将为write(*,*)dt%get(),其中get()将返回:

For example, the call would be write(*,*)dt%get() where get() would return :

  • dt%isInteger = .true.
  • 的整数
  • dt%isReal = .true.
  • 的实数
  • 如果是dt%isStr = .true.
  • ,则为字符
  • an integer if dt%isInteger = .true.
  • a real if dt%isReal = .true.
  • a character if dt%isStr = .true.

我相信可以通过使用抽象接口使过程get()指向过程getInteger()getReal()getStr()来实现,但是抽象接口定义需要定义输出类型,即我的情况,可变的.

I believe this might be possible by using an abstract interface to make procedure get() point to either procedure getInteger(), getReal() or getStr() but the abstract interface definition needs to define the ouput type which is, in my case, variable.

这是相关代码:

type :: dt
    real(dp) :: realValue
    integer :: integerValue
    character(*) :: strValue
    logical :: isReal, isInteger, isStr
    procedure(intf), pointer :: get
contains
    procedure :: getReal, getInteger, getStr
end type

abstract interface
    function intf(self)
        import dt
        class(dt) :: self
        ??? :: intf
    end function
end interface

有什么主意吗?

推荐答案

在Fortran中根本不可能.

That is simply impossible in Fortran.

您可以使用具有不同特定功能的通用接口,但是这些功能必须具有不同类型的参数(请参阅几个内部函数(如transfer()如何使用mold参数)).这称为TKR(类型,种类,等级)分辨率.通用函数 不能基于参数的值进行区分.

You can use a generic interface with different specific functions, but these functions must have arguments of different types (see how several intrinsic functions, like transfer() use a mold argument). This is called the TKR (type, kind, rank) resolution. Generic functions cannot be distinguished based on a value of an argument.

type :: dt
    real(dp) :: realValue
    integer :: integerValue
    character(*) :: strValue             !!!! <= THIS IS WRONG !!!!
    logical :: isReal, isInteger, isStr
contains
    generic :: get => getReal, getInteger, getStr
    procedure :: getReal, getInteger, getStr
end type

function getReal(self, mold)
  class(dt) :: self
  real, intent(in) :: mold
end function

function getInteger(self, mold)
  class(dt) :: self
  integer, intent(in) :: mold
end function

function getString(self, mold)
  class(dt) :: self
  character(*), intent(in) :: mold
end function

如您所见,调用get()时必须知道正确的类型.你这样称呼

As you see, you have to know the correct type when calling get(). You call it like

real_variable = object%get(1.0)
integer_variable = object%get(1)

还要注意不同长度的琴弦.我在上面标记了它.您可能需要character(:), allocatable.

Be also careful about strings of different lengths. I marked it above. You probably want character(:), allocatable.

您还可以使函数返回通用容器,然后从容器中提取值.甚至可以使用容器的重载分配直接完成提取.

You can make also function which returns a generic container and then extract the value from the container. The extraction could even be done directly using an overloaded assignment for the container.

您还可以返回一个无限多态变量(class(*)).

You could also just return an unlimited polymorphic variable (class(*)).

这篇关于一个Fortran函数/子例程,可以返回实数,整数或字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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