I would like to know how to create a function that either returns a real, an integer or a string.
For example, the call would be write(*,*)dt%get()
where get()
would return :
- an integer if
dt%isInteger = .true.
- a real if
dt%isReal = .true.
- a character if
dt%isStr = .true.
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.
Here is the related code:
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
Any idea ?
That is simply impossible in Fortran.
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 amold
argument). This is called the TKR (type, kind, rank) resolution. Generic functions cannot be distinguished based on a value of an argument.As you see, you have to know the correct type when calling
get()
. You call it likeBe 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.
You could also just return an unlimited polymorphic variable (
class(*)
).