Reputation: 423
I have two modules with an init subroutine that look as following:
module letter_A
contains
subroutine a_init()
*do something*
call my_function(1)
print *, "letter A init"
end module letter_A
module letter_B
contains
subroutine b_init()
*do something*
call my_function(2)
print *, "letter B init"
end module letter_B
I would like to create a supermodule and convert the modules A
and B
to submodules, and outsource as much functionality as possible to the supermodule to reduce the amount of redundant code in my application. Currently, the only way I can think of how to do this is as following:
module letter
contains
subroutine letter_init()
*do something*
#if defined(A)
a_init(1)
#elif defined(B)
b_init(2)
#endif
end module letter
submodule(letter) A
contains
subroutine a_init()
call my_function(1)
print *, "letter A init"
end module letter_A
...
However, this only works for this particular minimal example. If I have more function calls with more identical *do something*
blocks inbetween, it becomes harder to separate the redundant parts in a seperate module and it's also not very nice to have to pass 10+ values to an init function. Is there a more sophisticated way of solving this problem?
Upvotes: 0
Views: 129
Reputation: 2689
Maybe I am missing something important (?), but the answer to your question looks as simple as that to me:
subroutine letter_init(letter)
character(1), intent(in) :: letter
!*do something*
if (letter == 'A') then
call my_function(1)
else if (letter == 'B') then
call my_function(2)
end if
print *, "letter "//letter//" init"
end subroutine letter_init
Upvotes: 3