Type bound procedure overloading in Fortran 2003

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 13:02:28

This is more of an extended comment, but it appears that you're being forced to speculate because of a compiler failing. I've compiled the code with gfortran 4.8.3 without error, and with the expected result. Also, it seems to me that what you want to happen should happen.

The other consequence is that it appears that two separate derived types cannot have bound subroutines of the same name (as shown by the error message).

While both subroutines are called compute they are in separate modules and this is allowed, although your use statements make a call compute(...) (which you aren't doing) ambiguous. If the type definitions were in the same module then you would have to resort to the procedure :: compute => compute_typex trick, but call mytype1%compute would still be acceptable.

I would recommend that if you're exposing the compute subroutines through the type-binding then you have them as private in the module, or at the very least, don't explicitly use them. [That is, have use mod1, only : type1.]

As for whether you're stuck with type%... in the subroutines, then yes, I think you are. That said, under Fortran 2008 there is the associate construct

subroutine compute(this)
  class(type1), intent(inout) :: this
  associate (x => this%x, y => this%y)
    ...
  end associate
end subroutine

but that doesn't gain much in this case. And there are other terrible tricks which I won't detail.

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