Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 在类型绑定的基本过程中作为意图(inout)传递对象_Fortran_Parameter Passing_Type Bounds - Fatal编程技术网

Fortran 在类型绑定的基本过程中作为意图(inout)传递对象

Fortran 在类型绑定的基本过程中作为意图(inout)传递对象,fortran,parameter-passing,type-bounds,Fortran,Parameter Passing,Type Bounds,我想将文本添加到标量对象的组件中,而不管这个附加文本的形状如何 为了尝试这一点,我创建了一个元素过程,它有一个元素输入参数,但只有一个intent(inout)参数,即传递的对象 这里是一个MWE: module add_mod implicit none type obj_A character(len=:), allocatable :: Message contains procedure, pass(objA) :: add procedure

我想将文本添加到标量对象的组件中,而不管这个附加文本的形状如何

为了尝试这一点,我创建了一个元素过程,它有一个元素输入参数,但只有一个
intent(inout)
参数,即传递的对象

这里是一个MWE:

module add_mod
  implicit none

  type obj_A
    character(len=:), allocatable    :: Message
  contains
    procedure, pass(objA) :: add
    procedure, pass(objA) :: write
  end type

contains
   elemental subroutine add( objA, text )
      implicit none

      class(obj_A), intent(inout)                   :: objA
      character(len=*), intent(in)                 :: text

      objA%Message=objA%Message//text
   end subroutine add

    impure elemental subroutine write( objA, text )
      implicit none

      class(obj_A), intent(in)                   :: objA
      character(len=*), intent(in)                 :: text

      print*,'write ', text
   end subroutine write
end module


program test
  use add_mod
  implicit none

  type(obj_A) :: testA


  call testA%add('toto')
  print *, testA%Message

  ! call testA%add( ['toto','abcc','d,ef'] )
  print *, testA%Message

  call testA%write( ['toto','abcc','d,ef'] )

end program
如果我让注释行
调用testA%add(['toto','abcc','d,ef'])
,它就可以正常工作了。但如果我取消注释,则在编译过程中会出现错误

错误:元素子例程“add”的意图(INOUT)伪“objA”在(1)处的实际参数是标量,但另一个实际参数是数组`

我理解为什么
testA%write
调用正确,这是由于传递对象的
intent(in)
;在这种情况下,编译器理解一个参数是标量形状,另一个是数组形状

使用
testA%add(['toto','abcc','d,ef'])
,我还了解到它需要一个数组形状的
obj_A
作为
intent(inout)
,因为为输入提供的文本是标量。因此,这不是正确的方法


无论文本的形状如何,是否有正确的方法将文本添加到
obj_%消息中?

当使用
元素
子例程时,您可以提供数组输入和数组输出[然后以元素方式进行操作]。但是,您正在尝试将数组输入分配给标量输出(此处:
testA

如果使用大小为3的数组输出,则例程将按预期工作:

module add_mod
  implicit none

  type obj_A
    character(len=:), allocatable    :: Message
  contains
    procedure, pass(objA) :: add
  end type

contains
   elemental subroutine add( objA, text )
      implicit none

      class(obj_A), intent(inout)                   :: objA
      character(len=*),intent(in)                   :: text

      objA%Message=objA%Message//text
   end subroutine add
end module

program test
  use add_mod
  implicit none

  type(obj_A) :: testA
  type(obj_A) :: testB(3)

  call testA%add('toto')
  print *, testA%Message

  call testB%add( ['toto','abcc','d,ef'] )
  print *, testA%Message
  print *, testB(1)%Message, testB(2)%Message, testB(3)%Message
end program
下面是一个将字符串数组添加到标量输出的版本。请注意,由于这个星座,子例程不能是
elemental
。但是,它可以是纯的

module add_mod
  implicit none

  type obj_A
    character(len=:), allocatable    :: Message
  contains
    procedure, pass(objA) :: add
  end type

contains
   pure subroutine add( objA, text )
      implicit none

      class(obj_A), intent(inout)                   :: objA
      character(len=*), dimension(:), intent(in)    :: text
      integer :: i

      do i=1,size(text)
        objA%Message=objA%Message//text(i)
      enddo !i
   end subroutine add
end module

program test
  use add_mod
  implicit none

  type(obj_A) :: testA

  call testA%add(['toto'])
  print *, testA%Message

  call testA%add( ['toto','abcc','d,ef'] )
  print *, testA%Message
end program
最后,为了同时支持标量和数组参数,您需要提供并绑定多个实现,然后使用
通用接口以相同的名称提供它们:

module add_mod
  implicit none

  type obj_A
    character(len=:), allocatable    :: Message
  contains
    generic :: add => add1, add2
    procedure, pass(objA) :: add1
    procedure, pass(objA) :: add2
  end type

contains
   pure subroutine add1( objA, text )
      implicit none

      class(obj_A), intent(inout)                   :: objA
      character(len=*), dimension(:), intent(in)    :: text
      integer :: i

      do i=1,size(text)
        objA%Message=objA%Message//text(i)
      enddo 
   end subroutine add1

   pure subroutine add2( objA, text )
      implicit none

      class(obj_A), intent(inout)                   :: objA
      character(len=*), intent(in)                  :: text

      objA%Message=objA%Message//text
   end subroutine add2
end module

program test
  use add_mod
  implicit none

  type(obj_A) :: testA

  call testA%add('toto')
  print *, testA%Message

  call testA%add( ['toto','abcc','d,ef'] )
  print *, testA%Message
end program

我想在obj_a中包含的消息中添加一条文本,而不带任何关于此文本形状的信息。我看到的唯一解决方案是使用泛型过程,但我希望避免使用该解决方案。如果要同时处理标量和向量参数,我看不到避免使用泛型过程的方法。