bob.sacamento
bob.sacamento

Reputation: 6651

"Extending" a function in a Fortran derived type

I say "extending" because, forgive me, I'm not sure what the correct OOP terminology here is. I do not exactly want to override a function. I want a function in an inheriting derived type to do all of the work that the function of the same name does in the parent type, and then add to that. I want something like:

module foo1
  type :: bar1
  contains
    procedure :: f1
  end type bar1
contains
  subroutine bar1()
  ! do stuff
  end subroutine bar1
end module foo1

module foo2
  use foo1
  type, extends(bar1) :: bar2
  contains
    procedure :: f1
  end type bar2
contains
  subroutine f1()
    ! call parent f1
    ! do other stuff
  end subroutine f1
end module foo2

Is there a way to do this in Fortran?

Upvotes: 1

Views: 597

Answers (1)

You must do normal overriding and inside the new procedure you have to call the parent type procedure manually (in a way linked in a comment by ptb A Fortran analog to python's super()?).

subroutine f1(self)
  call self%bar1%f1 
  !the rest
end subroutine

Upvotes: 4

Related Questions