Question

In fortran 2003, is it possible to define a derived type which has a component of an abstract type? For example, as below, I want to define a type Sup having a component o_Abst of Abst type.

TYPE, ABSTRACT :: Abst
 CONTAINS 
   PROCEDURE(some_proc), deferred, pass :: some_proc 
   ..
END TYPE Abst

TYPE :: Sup
   PRIVATE
   CLASS(Abst) :: o_Abst
   ..
 CONTAINS
   PROCEDURE :: another_proc
END TYPE Sup

One problem I have already encountered is in writing a constructor for a Sup type object. I can not assign a value to the component o_Abst by intrinsic assignment with = (Intel compiler says, "In an intrinsic assignment statement, variable shall not be polymorphic."). Or can't I write a constructor for a Abst type object because a deferred type-bound procedure can not be properly overridden if an argument other than the passed object dummy argument is of abstract type, as far as I understand.

I would also be happy to hear about a work around that avoids the use of a type like Sup. If it is tempting to define a type with a component of an abstract type, what are alternative strategies in general?

Was it helpful?

Solution

A derived type may have an polymorphic component with abstract declared type. The component must have either the pointer attribute or the allocatable attribute.

Intrinsic assignment to a polymorphic object was not permitted in F2003 (it is permitted in F2008 if the object being assigned to has the allocatable attribute, but ifort 12.1 does not support that). In F2003 an ALLOCATE statement with a SOURCE specifier can be used to achieve more or less the same result.

You can construct objects that have a type that is a non-abstract extension of Abst (it does not make sense for the dynamic type of an object to be abstract, hence no structure constructor exists for Abst itself). There is no restriction on procedures that are bound to a type taking one or more arguments of abstract type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top