如何在抽象派生类型中设计 PRIVATE OVERRIDABLE 过程?

Bla*_*ohn 5 oop fortran private abstract

通常,抽象类型是后代派生类型的模型。包含在该类型中的过程如果被推迟,则应该是 PUBLIC,因为 PRIVATE 不能被模块本身引用或覆盖。但是,如果我希望每个后代都具有相同的功能 PRIVATE 过程来做某事(例如,测试其实例的初始化状态),我该如何设计 ABSTRACT 派生类型?我是否必须在每个后代中手动定义它们,还是可以在 ABSTRACT 中放置一个模型并在后代中实现它?下面是一个例子:

module test1
    implicit none

    private
    public :: my_numeric_type

    type, abstract :: my_numeric_type
    contains
        private
        procedure(op2), deferred :: add
        procedure(op2), deferred :: subtract
        generic, public :: operator(+) => add
        generic, public :: operator(-) => subtract
    end type my_numeric_type

    abstract interface
        function op2(a, b) result(r)
            import :: my_numeric_type
            class(my_numeric_type), intent(in) :: a, b
            class(my_numeric_type), allocatable :: r
        end function op2
    end interface
end module test1

module test2
    use test1, only: my_numeric_type
    implicit none

    type, extends(my_numeric_type) :: my_integer
        private
        integer :: value
    contains
    private
        procedure, public :: add => add_my_integer
        procedure, public :: subtract => subtract_my_integer
    end type my_integer

    contains

    function add_my_integer(a, b) result(r)
        class(my_integer),      intent(in)  :: a
        class(my_numeric_type), intent(in)  :: b
        class(my_numeric_type), allocatable :: r
        ! DO SOME ADDITION.
    end function add_my_integer

    function subtract_my_integer(a, b) result(r)
        class(my_integer),      intent(in)  :: a
        class(my_numeric_type), intent(in)  :: b
        class(my_numeric_type), allocatable :: r
        ! DO SOME SUBTRACTION.
    end function subtract_my_integer

end module test2
Run Code Online (Sandbox Code Playgroud)

在该示例中,后代派生类型 my_integer 是非抽象类型并继承了延迟绑定,因此必须覆盖过程(加法和减法)。但是程序在 my_numeric_type 中是私有定义的,也就是说它们不能在模块 test1 之外被覆盖,所以上面的 my_integer 类型是无效的。我该怎么办 ?

ver*_*rie 0

来自 Fortran 2018 标准草案,第 7.5.5 节,第 9 段:

公共类型绑定过程可通过该类型的任何可访问对象进行访问。私有类型绑定过程只能在包含类型定义的模块及其后代中访问。

这意味着可以实现private deferred过程,但前提是所实现的过程与延迟过程在同一模块中声明。

例如:

module m
  implicit none
  
  type, abstract :: Parent
  contains
    procedure(add_Parent), deferred, private :: add
    generic, public :: operator(+) => add
  end type
  
  abstract interface
    function add_Parent(lhs,rhs) result(output)
      import :: Parent
      class(Parent), intent(in)  :: lhs
      class(Parent), intent(in)  :: rhs
      class(Parent), allocatable :: output
    end function
  end interface
  
  type, extends(Parent) :: Child
    integer :: contents
  contains
    procedure, private :: add => add_Child
  end type
  
contains

function add_Child(lhs,rhs) result(output)
  class(Child),  intent(in)  :: lhs
  class(Parent), intent(in)  :: rhs
  class(Parent), allocatable :: output
  
  select type(rhs); type is(Child)
    output = Child(lhs%contents+rhs%contents)
  end select
end function

end module

program p
  use m
  
  type(Child)                :: a,b
  class(Parent), allocatable :: c
  
  a = Child(3)
  b = Child(5)
  c = a+b
  
  select type(c); type is(Child)
    write(*,*) c%contents
  end select
end program
Run Code Online (Sandbox Code Playgroud)

此约束(必须在同一模块中声明ParentChild)可能会导致文件过大,并可能导致依赖顺序问题。有几种方法可以改善这种情况:

  • 用于submodules分解模块。例如Parent可以有自己的子模块,并且每个都Child可以有自己的子模块。
  • 不要使用延迟过程,而是private使用应被视为私有的延迟过程的命名约定,例如尾随下划线(soadd_Parent_add_Child_)。