It is not possible directly. But why do you not create a base type which is then extended?
module definitions
implicit none
type base
integer i
contains
procedure,pass :: init
end type
type, extends(base) :: type1
end type
type, extends(base) :: type2
end type
contains
subroutine init(x)
class(base),intent(inout) :: x
x%i=3
end subroutine
end module
program test
use definitions
type(type1) ::a
type(type2) ::b
print*, a%i
print*, b%i
call a%init
print*, a%i
print*, b%i
call b%init
print*, a%i
print*, b%i
end program
Edit (PM): This solves the problem which I actually had in mind:
module definitions
implicit none
type base
integer :: i
contains
procedure,pass :: init
end type
type, extends(base) :: type1
integer:: j
end type
type, extends(base) :: type2
integer:: k
end type
contains
subroutine init(x)
class(base),intent(inout) :: x
integer :: m
select type (x)
type is (type1)
m=x%j
type is (type2)
m=x%k
end select
x%i=3*m
end subroutine
end module
program test
use definitions
type(type1) ::a
type(type2) ::b
a%j=2
b%k=4
print*, a%i
print*, b%i
call a%init
print*, a%i
print*, b%i
call b%init
print*, a%i
print*, b%i
end program
1,5 Top