带有可选参数的 Fortran 函数

Fortran Functions with optional arguments

我想使用运算符 .ef. 但是运算符不接受可选参数。是否可以保留我的功能并让操作员也工作?

Module Core
Implicit None

Interface Operator (.ef.)
  Module Procedure fes
End Interface Operator (.ef.)

Contains

Function fes    &
  (             &
    nm, wn      &
  )             &
    Result (located)

Logical :: located
Character (Len=*), Intent (In) :: nm
Character (Len=*), Intent (In), Optional :: wn 

End Function 

Gfortran 返回以下问题

lib/scriptus/core.f:62:0:

Function fes    &
1
Error: Second argument of operator interface at (1) cannot be optional

您不能对已定义的操作使用可选参数。 Fortran 2008,Cl。 12.4.3.4.2。说:

1 ... The dummy arguments shall be nonoptional dummy data objects ...

这是您的编译器在发出错误时引用的内容:

Error: Second argument of operator interface at (1) cannot be optional

注意:您可以拥有带有可选参数的过程,它们可以出现在模块中,但它们不能在带有 operator 关键字的接口块中引用。您的函数 fes 看起来不错,这不是问题所在。您的问题是接口块将运算符映射到函数。