I have a code similar to:
Module C_sys
use class_A
implicit none
Private
Type, public :: C_sys_type
private
logical :: Ao_set = .false.
type(A) :: Ao
Contains
Private
Procedure, public :: get_Ao
Procedure, public :: set_Ao
End Type C_sys_type
interface C_sys_type
Procedure C_sys_type_constructor
end interface C_sys_type
Contains
type(C_sys_type) elemental function C_sys_type_constructor(Ao) result(C_sys)
type(A), intent(in), optional :: Ao
C_sys % Ao = Ao
C_sys % Ao_set = .true.
end function C_sys_type_constructor
type(A) elemental function get_Ao
class(C_sys_type), intent(in) :: this
get_Ao = this % Ao
end function get_Ao
subroutine set_Ao(this, Ao)
class(C_sys_type), intent(inout) :: this
type(Ao), intent(in) :: Ao
this % Ao = Ao
this % Ao_set = .true.
end subroutine set_Ao
End Module C_sys
I am not sure where in the subroutine set_Ao , type(Ao), intent(in) :: Ao should be left like this or instead to have class(Ao), intent(in) :: Ao. I know that class(Ao) is making the variable polymorphic and accessing the data type of A. But I don't know when it has to be used one or the other.
Thanks.
If you want to be able to bind a function/subroutine to a derived type (and for that routine to be able to access/modify the members of an instance of that type, which is the usual use-case; referred to as "
PASS
ing" a variable), you need to meet the following conditions:TYPE
definition must contain an appropriatePROCEDURE
line (either withPASS
explicitly stated, or there by-default wheneverNOPASS
is not specified).TYPE
in question, which must be declared in the argument list withCLASS
(subject to all the restrictions that entails).CLASS
for this is that some otherTYPE
might "extend" yourTYPE
, which would mean it inherits its members - this can only work if the member routines are data-polymorphic.I've attempted to modify your provided code sample into something representative of what I think you actually meant, but which actually compiles, to hopefully demonstrate correct usage.
TYPE A
andTYPE AO
were defined in theCLASS_A
module you haven't provided. I've declared a dummy type in my version.Things can get more complex if you want to make use of
NOPASS
etc., but for "normal" usage I hope this answers your question.