如何将函数的结果作为派生类型类的元素?

时间:2019-03-31 15:39:08

标签: pointers types fortran

我正在尝试创建一个抽象元素,并且能够在不明确说明元素是谁的情况下工作;例如创建矢量并对其元素执行某些操作,而不管矢量是谁。

这是示例代码

module reso
use, intrinsic :: iso_fortran_env, only: dp => real64
implicit none

type datos  
real(dp) :: x1,x2
integer :: longitud
contains
end type datos

abstract interface
function WW(r)
import
type(datos), intent(in) :: r
real(dp),dimension(r%longitud) :: WW
end function
end interface 

type vector
contains
procedure(WW),nopass, pointer :: vec=>linspace
end type

contains
function linspace(r) result (v)
type(datos), intent(in) :: r
real(dp),dimension(r%longitud) :: v
integer :: i
real(dp) :: incremento 
incremento = (r%x2-r%x1)/(real(r%longitud-1))
forall (i=1:r%longitud) v(i) = incremento*(i-1)+r%x1
end function linspace

end module reso

module operation
use, intrinsic :: iso_fortran_env, only: dp => real64
use reso

implicit none

contains

function diff(r,c) result (dd)
type(vector),intent(in) :: r
type (datos),intent(in) :: c
real(dp), dimension(c%longitud-1) :: dd
integer:: i
do i = 1:c%longitud-1
dd(i) = r%vec(c)(i+1) - r%vec(c)(i)
end do
end function diff

end module operation


program main
use, intrinsic :: iso_fortran_env, only: dp => real64
use reso
use operation
type (datos) ::d
d%x1 = 5.0d0
d%x2 = 7.0d0
d%longitud = 3

type(vector) :: v

print*,  v%vec(d)
print*,  v%vec(d)(1)

print*, diff(v,d)

end program main

这些模块的主要思想是创建数组,我希望能够使用函数linspace的结果执行操作。这就是为什么我在type vector中创建指针的原因。但这无法正常工作,因为在用gfortran-8进行编译时出现以下错误:
   procedure(WW),nopass, pointer :: vec=>linspace 1 Error: Expected binding attribute at (1) 有没有办法正确和/或更整洁地做到这一点?

0 个答案:

没有答案