OOP Fortran, Type and Procedures in different files

隐身守侯 提交于 2021-02-11 05:52:35

问题


I was wondering whether it is possible to place the actual subroutines behind type bound procedures and the type definition in different files. EG:

File A:

Module TypeDef  
Type :: Test  
  Integer :: a,b,c  
contains  
  Procedure, Pass, Public :: add => SubAdd  
End Type  
Type(Test) :: Test  
  Interface  
   Subroutine SubAdd(this)  
      Import TypeDef  
      Class(TypeDef), Intent(InOut) :: this  
  End Subroutine  
  End Interface  
End Module  

File B:

Module TypeRoutines  
   use TypeDef  
   Private :: SubAdd
contains  
   Subroutine SubAdd(this)  
      Class(TypeDef), Intent(InOut) :: this  
      this%c=this%a+this%b  
   End Subroutine  
End Module  

This does not work because when compiling file A first and then file B, ifort gives the error message :

The name of the module procedure conflicts with a name in the encompassing scoping unit

The main reason this is that for some types I have to write lots of type bound procedures, and some files are expanding over hundreds of lines which makes work very tedious. The ultimate goal would be to place every single Subroutine into a different file.

Any ideas?


回答1:


Assuming that your bindings have a passed argument (if not, things are easier):

Easy! Place the body of the procedures into submodules of the TypeDef module and copy and edit the interface of the procedures into separate interface bodies in the specification part of the module itself. The only hitch is that it requires a F2008 compiler (one that supports submodules) and current ifort isn't one of those...

In the real world I think you only have the following options:

  • Bindings can be to external procedures with an explicit interface... so make each procedure that implements a specific binding an external procedure (not a module procedure as you have attempted), create an interface body for each external procedure in the specification part of the module (as you have done) and then associate each binding with its respective external procedure. This approach fails down if the type has private components or bindings that the external procedures need to access and care needs to be taken to make sure that the interface bodies match the actual interface of the external procedure.

or

  • Put each module procedure in a separate file, and then INCLUDE those separate files after the CONTAINS statement of the module.

or

  • Live with big files.


来源:https://stackoverflow.com/questions/19262948/oop-fortran-type-and-procedures-in-different-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!