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.

delta
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
  1. hits.value ← 10

    22hits = counter(10)23delta = 3
    values this step10hits.value
  2. delta ← 3

    22hits = counter(10)23delta = 324call hits%add(delta)
    values this step3delta
  3. hits.value ← 13

    23delta = 324call hits%add(delta)25print '(I0)', hits%value
    values this step10 13hits.value3delta
  4. print '(I0)', hits%value

    24    call hits%add(delta)25    print '(I0)', hits%value26end program type_bound_update_demo
    output13
    values this step13hits.value
  1. hits.value ← 10

    22hits = counter(10)23delta = 1
    values this step10hits.value
  2. delta ← 1

    22hits = counter(10)23delta = 124call hits%add(delta)
    values this step1delta
  3. hits.value ← 11

    23delta = 124call hits%add(delta)25print '(I0)', hits%value
    values this step10 11hits.value1delta
  4. print '(I0)', hits%value

    24    call hits%add(delta)25    print '(I0)', hits%value26end program type_bound_update_demo
    output11
    values this step11hits.value
  1. hits.value ← 10

    22hits = counter(10)23delta = 5
    values this step10hits.value
  2. delta ← 5

    22hits = counter(10)23delta = 524call hits%add(delta)
    values this step5delta
  3. hits.value ← 15

    23delta = 524call hits%add(delta)25print '(I0)', hits%value
    values this step10 15hits.value5delta
  4. print '(I0)', hits%value

    24    call hits%add(delta)25    print '(I0)', hits%value26end program type_bound_update_demo
    output15
    values 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.