Module, Interface and Derived type

Module, Interface and Derived type

Hi,

I would like to write a module which integrate the a derived type definition, some subroutines using this type and their interfaces. Here is an example of what my code looks like :

MODULE MY_MODULE
 
TYPE my_type
  character(1)      :: name
  integer           :: i,j  
ENDTYPE my_type

INTERFACE
  SUBROUTINE my_subroutine(x,my_type_1)
  IMPORT my_type
  double precision    :: x
  type(my_type)       :: my_type_1
  ENDSUBROUTINE my_subroutine
ENDINTERFACE

CONTAINS
  SUBROUTINE my_subroutine(x,my_type_1)
    implicit none
    double precision    :: x
    type(my_type)       :: my_type_1
    
    print*,x
    print*,my_type_1
  ENDSUBROUTINE my_subroutine
 
ENDMODULE MY_MODULE

When I want to compile this module, i've got an error :

error #6645: The name of the module procedure conflicts with a name in the encompassing scoping unit.   [MY_SUBROUTINE]

I don't know why there is this conflict. Any idea ..?

 

4 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

A module procedure (a procedure that appears after the CONTAINS in a module) automatically gets an explicit interface - that interface is available anywhere that module is USE'd.  There is no need for the programmer to describe the module procedure in an interface block - in nearly all cases it is an outright error to do so.

(The interface block that you have at module scope actually tells the compiler that there is an external procedure defined somewhere else called my_subroutine.  The name of that procedure conflicts with the name of your module procedure.  A name cannot refer to two things at once in the same scope - hence the error.)

Thanks for your reply, I don't used to work with interfaces. I now understand where is the conflict. But if I want to define an interface for my subroutine in this module (to use keyword arguments, for example), is it possible ?

Because your procedure is a module procedure, an interface block is not necessary.  The module procedure already, by the rules of the language, "magically" has an interface.   After it has compiled the module the compiler knows everything about the procedure (argument names, argument types, argument rank, argument INTENT, etc).  When you USE that module all that information becomes available in the scope that has the USE statement.

This is one of the great benefits of modules - the compiler automatically knows the interface of the procedure - you don't have to provide it manually in an interface block.

A procedure can only have one interface.  A module procedure automatically gets that one interface. Providing an interface block for a module procedure is an error. 

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui