Fortran FINAL procedure for ABSTRACT type

佐手、 提交于 2020-02-24 17:01:13

问题


Can I add a final procedure to an abstract type?

Suppose the final procedure looks like this:

subroutine finalize(this)
   type(bin_tree_t), intent(inout) :: this

   deallocate(this%head)
end subroutine finalize

My compiler (ifort 18.0.1) gives "error #8313: The TYPE(derived-type-spec) shall not specify an abstract type". I get this, but the dummy argument of a final subroutine cannot be polymorphic.

If this is not possible, is it then likely to have been a conscious choice of the standards committee, or just an oversight?


回答1:


It is not possible for an abstract type to have a final subroutine. As you note, the argument for such a subroutine must not be polymorphic and we can't instantiate an abstract type with type(spec).

When an object is finalized, the final subroutine selected is one with argument type the dynamic type of the object to be finalized. No object may have dynamic type an abstract type. Final subroutines are not inherited (and may not be overridden).

I can imagine use cases where a final subroutine for an abstract type would be nice. As it is, any finalization we'd want the abstract type to perform should be done by the extending type. Note that finalizable components of the abstract parent will still be finalized.

I can't comment on the thinking of the standards committee, but those cases don't seem to have greater value than the simple specification of finalization that we now have.


I said that the argument for the final subroutine must not be polymorphic. This is C480 of the Fortran 2008 standard. It's a perfectly reasonable question to ask "what problems are caused by allowing a polymorphic argument?".

The finalization process as we have it doesn't express the chosen final subroutine for an entity in terms of generic resolution, overriding or inheritance. The choice is "if there's a suitable subroutine for the dynamic type, take it". Instead, if polymorphic arguments were allowed: "if there's a suitable subroutine for the dynamic type, take it; if not, iterate through parent types...". This latter concept doesn't appear much elsewhere.

Note that the parent type is finalized once the extending type has been finalized (even if that extending type has no corresponding final subroutine).

In conclusion, it seems to me that there's no active desire to prohibit final subroutines for abstract types but the cost to allow them was too great. The current approach is quite simply defined. Could finalization have been written in a different way? Yes. Alas, we'd need someone with more detailed knowledge to give us details on why this particular way was chosen.



来源:https://stackoverflow.com/questions/48788976/fortran-final-procedure-for-abstract-type

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