1

My library has modules that specify abstract interfaces for certain subroutines, something like this:

module abstract_module
    implicit none

    ! interface of subroutines
    abstract interface
        subroutine sub_interface(...)
           ...
        end subroutine
    end interface

end module

Now in my program I have written a subroutine, and in order to use it properly, I have to declare it and it works just fine:

program testsub
    use abstract_module
    ...
    implicit none

    ! custom interface
    procedure(sub_interface) :: custom

    ! call the subroutine via another one
    call test_call(custom)
end program

Now I'd like to gather all custom subroutines into a module, but I don't know how I could specify that a subroutine is, in fact, adhering to an interface:

module custom_subs
    use abstract_module
    implicit none

    ! this does not compile, obviously
    abstract interface
        procedure(sub_interface) :: custom
    end interface

contains

subroutine custom(...)
   ...
end subroutine

end module

Is there a way of specifying in the module the subroutines as I have done in the program, or do I have to leave them in the program itself?

senseiwa
  • 2,369
  • 3
  • 24
  • 47
  • 1
    I'm afraid I don't quite follow what you want to do. You can put the subroutines into module `custom_subs` but in this case you don't need to additionally provide (abstract) interface blocks: the module already has these subprograms. Is it that you started off with lots of external procedures then decided you wanted to provide interfaces for them (so created a module with the interface blocks); now you've decided to put the external procedures in a module? – francescalus Nov 29 '19 at 11:32
  • @francescalus Yes, they were external, used as you can see in the `program`. Now I’d like to move them into a module, which I understand is the “correct” way. – senseiwa Nov 29 '19 at 12:49

1 Answers1

3

You have a bunch of external procedures and you want to provide explicit interfaces for them when they are being used. You are considering two approaches:

  • using interface blocks
  • making the procedures themselves module procedures

These are two different and incompatible approaches. You must choose one or the other; you cannot mix them for the same procedure.

Converting the external procedures to module procedures is a laudable aim. Consider the module

module custom_subs
  implicit none

contains

  subroutine custom(...)
     ...
  end subroutine

end module

That is, just put the procedures into the module: don't worry about using this other abstract_module module, or adding interface blocks. Indeed, destroy abstract_module - you no longer have custom as an external procedure, so the interface to it as one won't be resolvable.

This answer just aims to resolve the confusion around whether the approaches are complementary. Much more detail on each approach can be found in other questions such as this one and this one.

francescalus
  • 30,576
  • 16
  • 61
  • 96