Search code examples
oopfortrandeferredabstract-data-type

Fortran 2003, can data be deferred in an abstract type?


I know it's possible to defer the definition of procedures from an abstract type to its derived types. Is it possible to include 'deferred' data in an abstract type, i.e., data whose type and value is only defined in derived classes?

The closest question I found on stackoverflow was here. It does not address my needs.

If clarification is needed, please ask. Many thanks.


Solution

  • There's no straightforward way to defer the definition of a data component of an (abstract) derived type as there is for procedure components, so no declaration such as

    type(magic), deferred :: element
    

    which can be overridden by a concrete declaration in an extended type. I think the easy (?) workaround would be to use class in the declaration. For ultimate flexibility you could use an unlimited polymorphic component, eg

    type :: stype
        class(*), allocatable :: element
    end type style
    

    What you can't then do is specify the type in a concrete extended type with a (re-)declaration something like

    type, extends(stype) :: mstype
        integer :: element
    end type mstype
    

    Instead, if you want to define an extended type which has an integer element you would create the type and write a constructor for it that ensures its element is allocated with type integer.

    If your requirements are more modest the 2003 feature of parameterised derived types might satisfy you, but as far as I know only the Cray and IBM XL compilers implement that yet.