Context
Suppose we have several modules of the general form:
module abc_m
⋮
type abc_t
integer :: int1
real :: real1
⋮
end type abc_t
contains
subroutine abc_sub1_i(this, a, …)
⋮
end subroutine abc_sub1_i
subroutine abc_sub1_r(this, a, …)
⋮
end subroutine abc_sub1_r
subroutine abc_sub2(this, a, …)
⋮
end subroutine abc_sub2
⋮
end module abc_m
Some of the procedures are similar for different types, e.g. create, destroy, print.
One option to streamline the usage would be to add interfaces to each module:
interface init
module procedure :: abc_init
end interface init
interface sub1
module procedure :: abc_sub1_i
module procedure :: abc_sub1_r
end interface sub1
⋮
(I was a bit surprised that you can define interfaces with the same name at different locations.)
Alternatively one could use type-bound procedures:
type abc_t
⋮
contains
procedure :: init => abc_init
procedure :: sub1 => abc_sub1_i, abc_sub1_r
⋮
end type abc_t
I like the type-bound approach as you only
have to import the type and get all the procedures without cluttering the namespace.
Questions
- Are there drawbacks to type-bound procedures or changing
type(abc_t)
toclass(abc_t)
in the procedures? - I suppose the type-bound approach would be called object-oriented (even if we ignore interitance and polymorphism here), is there a name for the interface approach?
- Do you have any thoughts on this topic?