(j3.2006) Interp request for GENERIC

Jim Xia jimxia
Thu Mar 3 23:13:14 EST 2011


There is a typo in your code:        procedure :: base_r8 ! shouldn't this 
be base_r?

A generic call is resolved to a specific binding name at compile time and 
bound to a specific procedure at runtime.  So bar%proc(i) and bar%proc(x) 
are resolved to as if calling bar%base_i(i) and bar%base_r(x) respectively 
at compile time.  The actual procedures are invoked are dependent on the 
dynamic type of the designator -- in this case a Child type.  So the 
actual procedures called are child_i and child_r.  The intuitive sense is 
correct :-)

Cheers,

Jim Xia

Compiler Testing, X10 & XLF
IBM Toronto Lab at 8200 Warden Ave,
Markham, On, L6G 1C7
905-413-3444




From:
Tom Clune <Thomas.L.Clune at nasa.gov>
To:
fortran standards email list for J3 <j3 at j3-fortran.org>
Date:
03/03/2011 10:54 PM
Subject:
(j3.2006) Interp request for GENERIC



In the sample code below there are two derived types defined - Base 
and Child, which EXTEND's Base.   Base defines a GENERIC with two type- 
bound procedures, and Child overrides those type bound procedures.  On 
two compilers, when the generic is invoked on an object of type child, 
the Child's type-bound procedures are invoked, while one other the 
parent's are invoked.   The former makes more intuitive sense.   The 
statements on the subject in the extant F2003 books left me more 
confused on this issue.

module ClassMod
    implicit none

    type Base
    contains
       procedure :: base_i
       procedure :: base_r8
       generic :: proc => base_i, base_r
    end type Base

    type, extends(Base) :: Child
       integer :: i
       real :: x
    contains
       procedure :: base_i  => child_i
       procedure :: base_r => child_r
    end type Child

contains

    subroutine base_i(this,i)
       class(Base) :: this
       integer :: i
       write(6,*) 'executed base_i'
    end subroutine base_i

    subroutine base_r(this,x)
       class(Base) :: this
       real :: x
       write(6,*) 'executed base_r'
    end subroutine base_r

    subroutine child_i(this,i)
       class(Child) :: this
       integer :: i
       this%i = i
       write(6,*) 'executed child_i'
    end subroutine child_i

    subroutine child_r(this,x)
       class(Child) :: this
       real :: x
       this%x = x
       write(6,*) 'executed child_r'
    end subroutine child_r

end module ClassMod

program tryit
    use ClassMod
    implicit none
    integer :: i
    real :: x
    type(Base) :: foo
    type(Child) :: bar
    i = 5
    x = 20e0
    write(6,*) 'calling Base:'
    call foo%proc(i)
    call foo%proc(x)
    write(6,*) 'calling actual procedures:'
    call bar%proc(i)
    call bar%proc(x)
end program tryit



Thanks in advance,

- Tom

Thomas Clune  
Software Integration and Visualization Office
NASA GSFC (610.3)
301-286-4635 (W)                 301-286-1775 (F)
<Thomas.L.Clune at nasa.gov>






_______________________________________________
J3 mailing list
J3 at j3-fortran.org
http://j3-fortran.org/mailman/listinfo/j3


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://j3-fortran.org/pipermail/j3/attachments/20110303/30c703fa/attachment.htm>



More information about the J3 mailing list