Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OOP and GENERICS statement.

16 views
Skip to first unread message

sfilippone

unread,
Apr 29, 2010, 3:31:37 PM4/29/10
to
Hello,
Consider the following code:
------------------- testd15.f03----------------
module foo_mod
type foo
integer :: i
contains
procedure, pass(a) :: doit
procedure, pass(a) :: getit
generic, public :: do => doit
generic, public :: get => getit
end type foo
private doit,getit
contains
subroutine doit(a)
class(foo) :: a
a%i = 1
write(*,*) 'FOO%DOIT base version'
end subroutine doit
function getit(a) result(res)
class(foo) :: a
integer :: res
res = a%i
end function getit
end module foo_mod

module foo2_mod
use foo_mod
type, extends(foo) :: foo2
integer :: j
contains
procedure, pass(a) :: doit => doit2
procedure, pass(a) :: getit => getit2
!!$ generic, public :: do => doit
!!$ generic, public :: get => getit
end type foo2
private doit2, getit2

contains

subroutine doit2(a)
class(foo2) :: a
a%i = 2
a%j = 3
write(*,*) 'FOO2%DOIT derived version'
end subroutine doit2
function getit2(a) result(res)
class(foo2) :: a
integer :: res
res = a%j
end function getit2
end module foo2_mod

program testd15
use foo2_mod
type(foo2) :: af2

call af2%do()
write(*,*) 'Getit value : ', af2%get()

end program testd15
-------------------------------------------------------------

With a development version of GNU fortran I get the following
output:
[sfilippo@localhost bug15]$ ./testd15
FOO%DOIT base version
Getit value : 1

If I uncomment the two GENERIC statements in module FOO2 I get
[sfilippo@localhost bug15]$ ./testd15
FOO2%DOIT derived version
Getit value : 3
which is what I expected; if I compile with NAG 5.2 I get the second
result in both cases.

So, is the second set of GENERIC statement required by the standard or
not? Or is it unspecified behaviour?

Thanks a lot

Salvatore

Jim Xia

unread,
Apr 29, 2010, 9:38:41 PM4/29/10
to


Nag compiler gets it right. This seems to be a bug in gFortran. The
GENERIC is Fortran is designed as such that the invocation is resolved
at compile time to a specific binding. In your case, af2%do() and
af2%get() both are solved to call binding a2f%doit() and af2%getit().
These two calls are equvilant to af2%doit() and af2%getit(). And
based on the dynamic type of af2, routines doit2() and getit2() are
both called.

Cheers,

Jim

Salvatore

unread,
Apr 30, 2010, 4:17:50 AM4/30/10
to

Many thanks.
Salvatore

0 new messages