>> real(KND) pure function times2(x)
>> real(KND),intent(in) :: x
>> times2=times2*2
>> end function times2
>
> Try: "times2 = x*2" instead of multiplying times2 by 2.
>
> Tobias
Thanks Tobias, so stupid. Strangely it did output 4, which is the correct
answer.
I came to yet another issue. Gfortran-4.7 behaves strange or I got
something wrong with allocatables. In this version and I don't have any
other compiler to test it. It works fine if I declare Compose%inner as
pointer, but not with allocatable. The procedure pointer h%inner%f is null
even if it was correct inside the procedure NewComposeFun and the program
fails on the last line.
Vladimir
module Parameters
implicit none
integer,parameter :: KND = kind(1.)
end module Parameters
module AritmFunctions
use Parameters
implicit none
abstract interface
real(KND) function fce(x)
import KND
real(KND),intent(in) :: x
end function fce
end interface
contains
real(KND) function plus1(x)
real(KND),intent(in) :: x
plus1=x+1
end function plus1
real(KND) function times2(x)
real(KND),intent(in) :: x
times2=x*2
end function times2
endmodule AritmFunctions
module ComposeObj
use Parameters
use AritmFunctions
implicit none
private
public Compose,Fx
type :: Fx
! private
procedure(fce),pointer,nopass :: f => null()
contains
procedure,public :: call => Fx_call
end type Fx
type,extends(Fx) :: Compose
! private
class(Fx),allocatable :: inner
contains
procedure,public :: call => Compose_call
end type Compose
interface Fx
procedure NewFx
end interface
interface Compose
procedure NewComposeFun
! procedure NewComposeObj
end interface
contains
function NewFx(f)
procedure(fce) :: f
type(Fx) :: NewFx
write(*,*) "allocating Fx", loc(f)
NewFx%f => f
end function NewFx
real(KND) function Fx_call(this,x)
class(Fx),intent(in) :: this
real(KND),intent(in) :: x
print *, "call object Fx"
write(*,*) loc(this%f)
write(*,*) associated(this%f)
Fx_call = this%f(x)
end function Fx_call
function NewComposeFun(f,g)
procedure(fce) :: f,g
type(Compose) :: NewComposeFun
write(*,*) "allocating Composition",loc(f),loc(g)
NewComposeFun%f => f
allocate(NewComposeFun%inner,source= Fx(g))
write(*,*) loc(NewComposeFun%inner%f)
end function NewComposeFun
function NewComposeObj(f,g)
class(Fx) :: f,g
type(Compose) :: NewComposeObj
NewComposeObj%f => f%f
allocate(NewComposeObj%inner, source=g)
end function NewComposeObj
real(KND) function Compose_call(this,x)
class(Compose),intent(in) :: this
real(KND),intent(in) :: x
real(KND) :: y
write(*,*) loc(this%inner)
y=this%inner%call(x)
Compose_call = this%f(y)
end function Compose_call
end module ComposeObj
program Main
use Parameters
use AritmFunctions
use ComposeObj
implicit none
class(Compose),allocatable :: f,g,h,h2
print *, times2(plus1(1._KND))
write(*,*) "loc times2"
write(*,*) loc(times2)
write(*,*) "loc plus1"
write(*,*) loc(plus1)
allocate(h,source= Compose(times2,plus1))
write(*,*) loc(h),loc(h%f),loc(h%inner),loc(h%inner%f)