From: ksmith on
Hi,

Below is a simplified example of redeclaring a derived type exactly.
I'm wondering if it is standards conforming, since some compilers
allow it and others don't.

!-------------------------------------------------
subroutine outer(a)
implicit none
type :: atype
real :: r1
end type atype
type(atype), intent(inout) :: a

call inner(a)

contains

subroutine inner(a)
implicit none
type :: atype
real :: r1
end type atype
type(atype), intent(inout) :: a

a%r1 = 1.0

end subroutine inner

end subroutine outer
!-------------------------------------------------

The example is contrived, but illustrates the core of my question.
The books I've consulted are ambiguous.

Thanks in advance,

Kurt
From: m_b_metcalf on
On Feb 24, 6:48 pm, ksmith <kwmsm...(a)gmail.com> wrote:
> Hi,
>
> Below is a simplified example of redeclaring a derived type exactly.
> I'm wondering if it is standards conforming, since some compilers
> allow it and others don't.
>
> !-------------------------------------------------
>       subroutine outer(a)
>         implicit none
>         type :: atype
>             real :: r1
>         end type atype
>         type(atype), intent(inout) :: a
>
>         call inner(a)
>
>         contains
>
>         subroutine inner(a)
>             implicit none
>             type :: atype
>                 real :: r1
>             end type atype
>             type(atype), intent(inout) :: a
>
>             a%r1 = 1.0
>
>         end subroutine inner
>
>       end subroutine outer
> !-------------------------------------------------
>
> The example is contrived, but illustrates the core of my question.
> The books I've consulted are ambiguous.
>
> Thanks in advance,
>
> Kurt

Your example is not standard conforming -- it defines two different
types (although they are identical in every respect). See also the
last paragraph of Section 7.11 of "Fortran 95/2003 Explained". A
compiler that 'allows' it is in error.

Regards,

Mike Metcalf
From: Arjen Markus on
On 24 feb, 19:58, m_b_metcalf <michaelmetc...(a)compuserve.com> wrote:
> On Feb 24, 6:48 pm, ksmith <kwmsm...(a)gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > Below is a simplified example of redeclaring a derived type exactly.
> > I'm wondering if it is standards conforming, since some compilers
> > allow it and others don't.
>
> > !-------------------------------------------------
> >       subroutine outer(a)
> >         implicit none
> >         type :: atype
> >             real :: r1
> >         end type atype
> >         type(atype), intent(inout) :: a
>
> >         call inner(a)
>
> >         contains
>
> >         subroutine inner(a)
> >             implicit none
> >             type :: atype
> >                 real :: r1
> >             end type atype
> >             type(atype), intent(inout) :: a
>
> >             a%r1 = 1.0
>
> >         end subroutine inner
>
> >       end subroutine outer
> > !-------------------------------------------------
>
> > The example is contrived, but illustrates the core of my question.
> > The books I've consulted are ambiguous.
>
> > Thanks in advance,
>
> > Kurt
>
> Your example is not standard conforming -- it defines two different
> types (although they are identical in every respect). See also the
> last paragraph of Section 7.11 of "Fortran 95/2003 Explained". A
> compiler that 'allows' it is in error.
>
> Regards,
>
> Mike Metcalf

The usual solution is to put the definition of the derived type in
a separate module and use that - also within the interface block.

Regards,

Arjen
From: Craig Powers on
m_b_metcalf wrote:
> On Feb 24, 6:48 pm, ksmith <kwmsm...(a)gmail.com> wrote:
>> Hi,
>>
>> Below is a simplified example of redeclaring a derived type exactly.
>> I'm wondering if it is standards conforming, since some compilers
>> allow it and others don't.
>>
>> !-------------------------------------------------
>> subroutine outer(a)
>> implicit none
>> type :: atype
>> real :: r1
>> end type atype
>> type(atype), intent(inout) :: a
>>
>> call inner(a)
>>
>> contains
>>
>> subroutine inner(a)
>> implicit none
>> type :: atype
>> real :: r1
>> end type atype
>> type(atype), intent(inout) :: a
>>
>> a%r1 = 1.0
>>
>> end subroutine inner
>>
>> end subroutine outer
>> !-------------------------------------------------
>>
>> The example is contrived, but illustrates the core of my question.
>> The books I've consulted are ambiguous.
>
> Your example is not standard conforming -- it defines two different
> types (although they are identical in every respect). See also the
> last paragraph of Section 7.11 of "Fortran 95/2003 Explained". A
> compiler that 'allows' it is in error.


To fix the example so that both are the same type, add SEQUENCE to both
declarations.
From: ksmith on
On Feb 24, 12:58 pm, m_b_metcalf <michaelmetc...(a)compuserve.com>
wrote:
> On Feb 24, 6:48 pm, ksmith <kwmsm...(a)gmail.com> wrote:
>
> Your example is not standard conforming -- it defines two different
> types (although they are identical in every respect). See also the
> last paragraph of Section 7.11 of "Fortran 95/2003 Explained". A
> compiler that 'allows' it is in error.

Thanks for the clarification. And thanks for the book -- it is an
excellent reference.

My usecase is for a program I'm creating that automatically generates
C wrappers for fortran code, leveraging the ISO C BINDING module & the
C binding functionality. I'm running into some snags with derived
types, but the restrictions make sense. I want to be sure that the
generated code is standard conforming.

Thanks again,

Kurt