如何为每个类型扩展原始类型的过程制作一个具有不同输入参数的过程

时间:2019-05-07 15:21:47

标签: fortran gfortran

我有一个派生类型:

module type_1_mod
  implicit none

  public :: type1

  type :: type1
    private
    integer :: a, b
  contains
    procedure, public :: mandatoryProcedureForAllExtends
  end type type1

contains

  subroutine mandatoryProcedureForAllExtends(this, varInt, varReal, varLogic)
    class(type1) :: this
    integer :: varInt
    real :: varReal
    logical :: varLogic

    *** something ***
  end subroutine mandatoryProcedureForAllExtends

end module type_1_mod

我想声明其他派生类型,这些派生类型将扩展我上面写的该类型,并且所有这些类型都将具有过程mandatoryProcedureForAllExtends,但是我希望该过程没有强制性结构-意味着用户可以覆盖由另一个使用不同输入参数的过程,例如:

module type_2_mod
  use type_1_mod
  implicit none

  public :: type2

  type, extends(type1) :: type2
    private
  contains
    procedure, public :: mandatoryProcedureForAllExtends
  end type type2

contains

  subroutine mandatoryProcedureForAllExtends(this, varReal, varReal2, varReal3, varReal4, varLogic)
    class(type2) :: this
    real :: varReal, varReal2, varReal3, varReal4,
    logical :: varLogic

    *** something ***
  end subroutine mandatoryProcedureForAllExtends

end module type_2_mod

因此,扩展类型1的类型具有一个具有相同名称的过程,但将其重新定义为具有不同数量和类型的变量。

我试图将type1编写为摘要并使用延迟的过程,但是这些都需要使用过程结构来显式声明一个接口。我也尝试过使用类似

的方法

关于type1声明

  contains
    procedure, public :: type1VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type1VersionOfMandatory

contains

  subroutine type1VersionOfMandatory(this, varInt, varReal, varLogic)
    class(type1) :: this
    integer :: varInt
    real :: varReal
    logical :: varLogic

    *** something ***
  end subroutine type1VersionOfMandatory

关于type2声明

  contains
    procedure, public :: type2VersionOfMandatory
    generic :: mandatoryProcedureForAllExtends => type2VersionOfMandatory

contains

  subroutine type2VersionOfMandatory(this, varReal, varReal2, varReal3, varReal4, varLogic)
    class(type2) :: this
    real :: varReal, varReal2, varReal3, varReal4,
    logical :: varLogic

    *** something ***
  end subroutine type2VersionOfMandatory

但是尝试编译时收到错误:Error: Undefined specific binding ‘type1VersionOfMandatory’ as target of GENERIC ‘mandatoryProcedureForAllExtends’ at (1)Error: Undefined specific binding ‘type2VersionOfMandatory’ as target of GENERIC ‘mandatoryProcedureForAllExtends’ at (1)

我还尝试通过再次用相同的名称定义该过程来简单地覆盖该过程,但是出现了类似Error: Dummy argument ‘varInt’ of ‘mandatoryProcedureForAllExtends’ at (1) should be named ‘varReal’ as to match the corresponding argument of the overridden procedure

的错误

因此,在这一点上,我什至不确定是否可以使用fortran做到这一点。我更喜欢type1而不是Abstract类型,但是如果需要的话,一切都可以。

0 个答案:

没有答案