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