Creating heterogeneous arrays in Fortran

后端 未结 1 691
孤城傲影
孤城傲影 2020-12-11 05:22

I am trying to create heterogeneous arrays that contain variables of different types, for example, [ 1.0, 7, \"hi\" ]. I tried to include class(*)

相关标签:
1条回答
  • 2020-12-11 06:04

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

    0 讨论(0)
提交回复
热议问题