Fortran syntax for assignments

馋奶兔 提交于 2019-12-06 14:46:16

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

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.

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

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 Precisely, the second argument is the right-hand side enclosed in parentheses.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!