Fortran分配语法 [英] Fortran syntax for assignments

查看:107
本文介绍了Fortran分配语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran语法让我发疯了!谁能解释我怎么调用该任务(我很确定这也不是正确的术语...).我正在尝试根据值类型分配类型.我有以下内容:

The Fortran syntax is driving me mad! Can anyone explain how I can call the assignment (I'm pretty sure that is not the right terminology either...). I'm trying to assign a type according to the value type. I have the following:


module test_module

   implicit none

   type :: mytype

      integer   :: i
      real      :: r
      logical   :: l

   contains

      generic :: assignment(=) => mytype_to_type
      procedure, pass(me) :: mytype_to_type

   end type mytype

contains

   subroutine mytype_to_type(t, me)

      implicit none

      class(*), intent(inout)   :: t
      class(mytype), intent(in) :: me

      !.. process based on input type
      select type (t)
         type is (integer)
            t = me%i
         type is (real)
            t = me%r
         type is (logical)
            t = me%l
         class default
            stop "none"
            return
      end select

   end subroutine mytype_to_type

end module test_module

program test

    use test_module

    implicit none

    type(mytype) :: t_type

    integer :: i = 1
    real    :: r = 1.
    logical :: l = .true.

    t_type = i                 !! how is this supposed to work?

    select type(t_type)

        type is (integer)
            write(*,*) "is int"
        type is (real)
            write(*,*) "is real"
        type is (logical)
            write(*,*) "is logical"
        class default
            return

    end select


end program test

这还能行吗?有人可以帮我吗?

Would this even work? Could anyone help me with this?

谢谢!

推荐答案

在支持已定义赋值的子例程中,两个参数使得第一个参数对应于赋值语句的左侧,第二个参数对应于赋值语句的左侧. 1

In a subroutine supporting defined assignment the two arguments are such that the first corresponds to the left-hand side of the assignment statement and the second the right-hand side.1

在这里,您提供的子例程是从my_type表达式分配给无限多态对象的.这不是您想要的,在左侧看到t_type.

Here, then the subroutine you provide is assignment from a my_type expression to an unlimited polymorphic object. This isn't what you want, seeing t_type on the left.

相反,您应该为my_type对象提供定义的赋值 .

Instead, you should provide defined assignment to a my_type object.

subroutine stuff_to_mytype(me,t)
  class(mytype), intent(out) :: me
  class(*), intent(in) :: t

  !.. process based on input type
  select type (t)
     type is (integer)
        me%i = t
     type is (real)
        me%r = t
     type is (logical)
        me%l = t
     class default
        stop "none"
        return
  end select

end subroutine stuff_to_mytype

也就是说,您可以针对所支持的每种类型使用特定的子例程来执行此操作,而不是使用通用分辨率使用无限制的多态右侧.在这种情况下,您还可以考虑通用结构构造函数(t_type=mytype(i)).

That said, you could do this with a specific subroutine for each type you support, rather than an unlimited polymorphic right-hand side, with generic resolution. In this case you could also consider generic structure constructors (t_type=mytype(i)).

1 确切地说,第二个参数是用括号括起来的右侧.

1 Precisely, the second argument is the right-hand side enclosed in parentheses.

这篇关于Fortran分配语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆