Consider the following code.
MODULE a
TYPE:: concrete
END TYPE concrete
INTERFACE concrete
PROCEDURE constructor
END INTERFACE concrete
CONTAINS
SUBROUTINE constructor
END SUBROUTINE constructor
END MODULE a
As far as I understand, in this context the generic interface concrete
declares the constructor of type concrete
to be the subroutine constructor
(and this is the only way I know to declare a constructor). Now consider the following code.
MODULE a
TYPE, ABSTRACT:: abstract
END TYPE abstract
INTERFACE abstract
PROCEDURE what_am_i
END INTERFACE abstract
CONTAINS
SUBROUTINE what_am_i
END SUBROUTINE what_am_i
END MODULE a
Here, I have an abstract type and a generic interface with the same name, but there cannot be a constructor for the abstract type abstract
, by definition.
So my question is: is there any relation between an abstract type, a generic interface with the same name, and the procedure inside such interface (in the example, the subroutine what_am_i
)? If so, what is it?
There is exactly the same relationship between the type abstract
and the generic abstract
as there is between the type concrete
and the generic concrete
.
This relationship is, simply, "they have the same name". Although a generic function with the same name as a type can be viewed as a constructor, it has no real privileged position.
The definition of a derived type implies a default structure constructor with the same name as the derived type, so that
type t
integer i
end type t
type(t) x
x = t(1)
end
gives us an object x
of that type with the expected values.
If instead, we have x=t(1)
which can be resolved to a specific function f
we can call that function f
a constructor if it has function result an object of type t
.
But there's no reason at all why f
must return an object of type t
. Consider
module mod
implicit none
type t
end type t
interface t
module procedure f
end interface t
contains
function f()
integer f
f = 1
end function f
end module mod
use mod
implicit none
print *, t()
end
It's hard to call f
here a constructor.
That's it: no specific function under the generic abstract
can return a constructed abstract
object, but each can return something else. Just as a specific function under the generic concrete
can return something not of type concrete
.