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
Replay: real traced execution (multi-file project)
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 = 3
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 = 1
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 = 5
call hits%add(delta)
print '(I0)', hits%value
end program type_bound_update_demo
hits.value ← 10
22hits = counter(10)23delta = 3values this step10hits.valuedelta ← 3
22hits = counter(10)23delta = 324call hits%add(delta)values this step3deltahits.value ← 13
23delta = 324call hits%add(delta)25print '(I0)', hits%valuevalues this step10 → 13hits.value3deltaprint '(I0)', hits%value
24 call hits%add(delta)25 print '(I0)', hits%value26end program type_bound_update_demooutput13values this step13hits.value
hits.value ← 10
22hits = counter(10)23delta = 1values this step10hits.valuedelta ← 1
22hits = counter(10)23delta = 124call hits%add(delta)values this step1deltahits.value ← 11
23delta = 124call hits%add(delta)25print '(I0)', hits%valuevalues this step10 → 11hits.value1deltaprint '(I0)', hits%value
24 call hits%add(delta)25 print '(I0)', hits%value26end program type_bound_update_demooutput11values this step11hits.value
hits.value ← 10
22hits = counter(10)23delta = 5values this step10hits.valuedelta ← 5
22hits = counter(10)23delta = 524call hits%add(delta)values this step5deltahits.value ← 15
23delta = 524call hits%add(delta)25print '(I0)', hits%valuevalues this step10 → 15hits.value5deltaprint '(I0)', hits%value
24 call hits%add(delta)25 print '(I0)', hits%value26end program type_bound_update_demooutput15values this step15hits.value
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.