在Fortran中创建异构数组 [英] Creating heterogeneous arrays in Fortran

查看:120
本文介绍了在Fortran中创建异构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建包含不同类型变量(例如[ 1.0, 7, "hi" ])的异构数组.我试图在数组构造函数中包含class(*)type(*)(请参见下面的代码结尾),但是gfortran5.2只是将其视为语法错误.

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)?

更多详细信息:

以下代码是为什么我要创建这样一个数组的示例. checktype_multi例程使用optional关键字接收多个参数,但是由于参数数量固定,因此此方法显然受到限制.为了允许任意数量的参数,我尝试了checktype_array例程,但似乎无法传递具有不同类型的数组...更实用的

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

推荐答案

数组元素的值只能彼此不同.它们的类型或其他任何属性都不能不同.

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')])

(数组构造函数(或结构构造函数)指定值.值本身不能是多态的,值的类型始终只是值的类型.可选的 type-spec开头的语法反映了这一点,因为它只是一个 type-spec ,而不是 declaration-type-spec .)

(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.)

这篇关于在Fortran中创建异构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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