Creating heterogeneous arrays in Fortran

浪尽此生 提交于 2019-12-17 20:10:14

问题


I am trying to create heterogeneous arrays that contain variables of different types, for example, [ 1.0, 7, "hi" ]. I tried to include class(*) or type(*) in the array constructor (please see the end of the following code), but gfortran5.2 simply regards it as syntax error. Is there any way to make such an array with array constructor, or is it necessary to use a different approach (e.g., define a type that contains each element separately)?


More details:

The following code is an example why I want to create such an array. The checktype_multi routine receives multiple arguments with the optional keyword, but this approach is clearly limited because of the fixed number of arguments. To allow arbitrary number of arguments, I tried the checktype_array routine, but it seems not possible to pass an array with different types... A more practical case may be to make a subroutine for printing a variable number of arguments with various types.

module mymod
    implicit none
contains

    subroutine checktype ( x )
        class(*) :: x

        select type ( x )
            type is ( integer )      ; print *, "int    : ", x
            type is ( real )         ; print *, "real   : ", x
            type is ( character(*) ) ; print *, "string : ", x
        endselect
    end subroutine

    subroutine checktype_multi ( x1, x2, x3 )
        class(*), optional :: x1, x2, x3

        print *
        if ( present( x1 ) ) call checktype ( x1 )
        if ( present( x2 ) ) call checktype ( x2 )
        if ( present( x3 ) ) call checktype ( x3 )
    end subroutine

    subroutine checktype_array ( a )
        class(*) :: a(:)
        integer :: k

        print *
        do k = 1, size( a )
            call checktype ( a( k ) )
        enddo
    end subroutine

end module

program main
    use mymod

    call checktype_multi ( 1.0 )
    call checktype_multi ( 1.0, 7 )
    call checktype_multi ( 1.0, 7, "hi" )

    ! call checktype_array ( [ 1.0, 7, "hi" ] )  !! error (this is to be expected)

    !>>> Here is the problem.
    ! call checktype_array ( [ type(*)  :: 1.0, 7, "hi" ] )  !! this is also an error
    ! call checktype_array ( [ class(*) :: 1.0, 7, "hi" ] )  !! this too
end program

回答1:


The elements of an array may only differ from each other in value. They cannot differ in type, or any other attribute.

Instead, use a derived type wrapper around an unlimited polymorphic allocatable component. The dynamic type of the component is then considered part of the value of the object of the wrapper type.

TYPE :: wrapper
  CLASS(*), ALLOCATABLE :: item
END TYPE wrapper

CALL sub([wrapper(1), wrapper(2.0), wrapper('3')])

(An array constructor (or structure constructor) specifies a value. A value itself cannot be polymorphic, the type of the value is always just the type of the value. The syntax for optional leading type-spec in an array constructor reflects this, in that it is just a type-spec, not a declaration-type-spec.)



来源:https://stackoverflow.com/questions/33621185/creating-heterogeneous-arrays-in-fortran

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