How to avoid rewriting the same interface for a deferred type-bound procedure implemented in a submodule?

Viewed 109

Consider the following code, which defines an abstract type foo with a deferred procedure sub, and a type foo2 which extends foo:

MODULE m
  TYPE, ABSTRACT:: foo
  CONTAINS
    PROCEDURE(sub_int), DEFERRED:: sub
  END TYPE

  INTERFACE
    SUBROUTINE sub_int(THIS, x)
      IMPORT:: foo
      CLASS(foo), INTENT(IN):: THIS
      DOUBLE PRECISION, INTENT(INOUT):: x
    END SUBROUTINE
  END INTERFACE

  TYPE, EXTENDS(foo):: foo2
  CONTAINS
    PROCEDURE:: sub
  END TYPE

  INTERFACE
    MODULE SUBROUTINE sub(THIS, x)
      CLASS(foo2), INTENT(IN):: THIS
      DOUBLE PRECISION, INTENT(INOUT):: x
    END SUBROUTINE
  END INTERFACE
END MODULE

SUBMODULE (m) s
CONTAINS
  MODULE PROCEDURE sub
    x= x**2
  END PROCEDURE
END SUBMODULE

Is there a way to avoid writing the second interface?

I understand that it is needed in order to declare sub as a module procedure (otherwise the implementation would need to be done in the module, and not in the submodule), but is this the only way to do it?

In other words, is it possible to implement the procedure sub for foo2 without rewriting the entire interface for it?

1 Answers

It is not possible. A separate module subprogram requires an interface block, either in its defining submodule or an ancestor of its defining submodule.

Whether a procedure happens to be nominated by one (or more!) bindings of one (or more!) types is totally irrelevant.

Related