I was wondering if it is somehow possible to define a derived type in Fortran which automatically returns the right type, without calling the type specifically e.g. var%real? Here's an example to explain what I mean:
module DervType
implicit none
type, public :: mytype
real(8) :: r
integer :: i
logical :: l
end type
end module DervType
program TestType
use DervType
implicit none
type(mytype) :: test
test = 1. !! <-- I don't want to use test%r here
end program TestType
Would this be possible by defining some sort of interface assignment (overload the =) or something like that? Is this even possible?
Thanks! Any help appreciated!
test = xcan be done so that the component oftestset is in some way determined by the type ofx. However, is this what you mean? The part "which automatically returns the right type" suggests you may need to do something more exotic (likecall sub(test)acts likecall sub(test%r)if the argument is real, etc.).Error: Can't convert REAL(4) to TYPE(mytype), so this does not work. Sorry, I've looked at the example from your link but I don't understand it. I do need an interface right?