是否可以在 Fortran 2003 中模拟混合抽象/延迟和常规过程?

Ric*_*zes 4 fortran abstract-data-type

当我尝试在一个抽象类型中混合常规过程和延迟过程时,gfortran 拒绝任何常规过程的调用:“错误:(1) 处类型绑定过程调用的基对象是抽象类型‘tbody’”

    type, abstract  :: tBody
private
  ...
contains
  procedure                     :: init => new_Body
  ...
  procedure (contained), deferred   :: PointIn
end type tBody
abstract interface
  logical(LGT) pure function contained( Body, Point )
    import  :: tBody, tAffinePoint, LGT
    class(tBody), intent(IN)        :: Body
    type(tAffinePoint), intent(IN)  :: Point
  end function contained
end interface

subroutine newCuboid(  this, ... )
class(tCuboid), intent(OUT)     :: this
...

call this%tBody%init( ... )
....    [gfortran halts here]

end subroutine newCuboid
Run Code Online (Sandbox Code Playgroud)

有没有办法安排 tBody 类型,以便我可以同时拥有抽象的延迟过程和常规的实例化过程?

Ian*_*anH 5

不。

有一个简单的解决方案 - 替换call this%tBody%init(...)call new_Body(...)(您可能需要进行适当的可访问性更改)。

可能是无力的合理化 - 您没有根据引用的类型来解析过程(因为这是硬编码的),所以不要使用类型绑定过程语法。

在某些情况下,另一种解决方案是进一步拆分类型层次结构,以便抽象类型 tBody 具有一个非抽象父级,该父级承载“非延迟”过程的初始实现。