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
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 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
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)
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.
You could also just return an unlimited polymorphic variable (class(*)
).