Fortran 2003中的固有分配和多态性 [英] Intrinsic Assignment and Polymorphism in Fortran 2003

查看:97
本文介绍了Fortran 2003中的固有分配和多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将过程添加到此模块,由 @VladimirF 编写,它在Fortran 2003中实现了通用链接列表.我希望能够输出为了方便起见,将列表的内容作为一个数组,因此我在名为lists.f90的文件中的列表模块中添加了以下过程:

I tried adding a procedure to this module, written by @VladimirF, that implements a generic linked list in Fortran 2003. I wanted to be able to output the contents of the list as an array for convenience, so I added the following procedure to a lists module in a file called lists.f90:

  subroutine list_as_array(self, arrayOut)
    class(list),intent(inout) :: self
    class(*),dimension(1:self%length),intent(out) :: arrayOut
    integer :: i
    type(list_node), pointer :: nodeIter
    nodeIter = self%first
    do i = 1,self%length
      arrayOut(i) = nodeIter%item  ! <---ERROR here
      if (i<self%length) then
        nodeIter = nodeIter%next
      end if
    end do
  end subroutine list_as_array

ifort 18.0.0给出以下错误:

lists.f90(324): error #8304: In an intrinsic assignment statement, variable shall not be a non-allocatable polymorphic.   [ARRAYOUT]
      arrayOut(i) = nodeIter%item
------^

我对F2003 +中的多态性不熟悉,因此我不理解错误消息或其上下文.怎么了,怎么解决?

I'm new to polymorphism in F2003+, so I do not understand the error message or its context. What's wrong, and how can it be fixed?

推荐答案

我认为其他答案很好地描述了问题.一个不能分配给多态不可分配变量.而且数组元素是永远不会分配的.

I think other answers describe the problem well. One cannot assign to a polymorphic non-allocatable variable. And an array element is never allocatable.

我已经考虑过如何做这样的事情,但是我没有想出一个令人满意的解决方案,即如何在库中通用地创建这样的函数.主要的问题是没有类型保护程序可以检查两个实体的动态类型是否相同.您必须在SELECT TYPE中指定实际类型.

I have thought about how to do such a thing, but I did not come up with a satisfactory solution how create such a function generically within the library. The main problem is that there is no type guard that would just check that the dynamic types of two entities are the same. You have to specify the actual type in SELECT TYPE.

作为库的用户,一旦您知道所有元素都属于同一类型,便有两种可能性

As a user of the library, once you know that all elements are of the same type, you have two possibilities

  1. 使用同一库中的参数列表

  1. use the parametric list within the same library

为可能出现在数组中的每种类型创建自己的函数.不在库中,而是在您自己的代码中.您必须保证所有元素都属于该类型.

create your own function for each type that can appear in the array. Not in the library but in your own code. You must promise that all elements are of that type.

subroutine integer_list_as_array(self, arrayOut)
  class(list),intent(inout) :: self
  integer,dimension(1:self%length),intent(out) :: arrayOut
  ...
end subroutine integer_list_as_array

如果有人知道一个通​​用的技巧,并假设所有元素都属于同一类型,但未指定类型,我想知道这一点.

If anyone knows a trick to do that generically and just assume that all elements are of the same type, but without specifying the type, I would like to know that.

这篇关于Fortran 2003中的固有分配和多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆