Derived Type Procedures
Type-Bound Update
Mutating Self
A type-bound procedure can update the object when the passed object has intent(inout).
Program
Play the program to choose a delta and update a counter through a bound procedure.
type_bound_update.f90
module counter_model
implicit none
type :: counter
integer :: value
contains
procedure :: add
end type counter
contains
subroutine add(self, delta)
class(counter), intent(inout) :: self
integer, intent(in) :: delta
self%value = self%value + delta
end subroutine add
end module counter_model
program type_bound_update_demo
use counter_model
implicit none
type(counter) :: hits
integer :: delta
hits = counter(10)
delta =
call hits%add(delta)
print '(I0)', hits%value
end program type_bound_update_demo
module counter_model
implicit none
type :: counter
integer :: value
contains
procedure :: add
end type counter
contains
subroutine add(self, delta)
class(counter), intent(inout) :: self
integer, intent(in) :: delta
self%value = self%value + delta
end subroutine add
end module counter_model
program type_bound_update_demo
use counter_model
implicit none
type(counter) :: hits
integer :: delta
hits = counter(10)
delta =
call hits%add(delta)
print '(I0)', hits%value
end program type_bound_update_demo
module counter_model
implicit none
type :: counter
integer :: value
contains
procedure :: add
end type counter
contains
subroutine add(self, delta)
class(counter), intent(inout) :: self
integer, intent(in) :: delta
self%value = self%value + delta
end subroutine add
end module counter_model
program type_bound_update_demo
use counter_model
implicit none
type(counter) :: hits
integer :: delta
hits = counter(10)
delta =
call hits%add(delta)
print '(I0)', hits%value
end program type_bound_update_demo
intent(inout)
`intent(inout)` lets a procedure read and update the object.
subroutine method
`call hits%add(delta)` invokes a mutating type-bound subroutine.
component update
`self%value` names a component of the current object.