Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29343 ディスカッション

the assignment(=) leads to an error #8769

target
ビギナー
2,606件の閲覧回数
module wm

type:: w
private
character(:),allocatable:: w_
character(:),pointer:: wptr_
contains
!this assignment(=) leads to an error
generic:: assignment(=) => eq
procedure:: eq
end type w

contains

impure elemental subroutine eq(lhs, rhs)
class(w),target,intent(out):: lhs
class(w),intent(in):: rhs
if(allocated(rhs%w_)) then
lhs%w_ = rhs%w_
lhs%wPtr_ => lhs%w_
else
lhs%wPtr_ => rhs%wPtr_
endif
end subroutine eq

end module wm

program main
use wm
implicit none
class(*),allocatable:: a
type(w):: u

a = u

contains

end program main

 

i wanna make a flexible character type(w), which can be used as either allocatable or pointer, and can be stored in an hashmap with val=[class(*),allocatable], however the assignment(=) procedure stop me.

the error message at line(34) : error #8769: If the actual argument is unlimited polymorphic, the corresponding dummy argument must also be unlimited polymorphic. [A]

what can i do ?

0 件の賞賛
1 解決策
FortranFan
名誉コントリビューター III
2,562件の閲覧回数
@target writes:
.. when x, y are both type(w), and write x = y

the assignment make sure that if y is a pointer(which means allocated(y%w_)=false), x is a pointer and x%wptr_ => y%wptr_; if y is not a pointer, x is also not and x%wptr_ => x%w_(the native assignment can't do that) ..

@target,

You appear to be interested in proper Generics facility in Fortran which it does not provide.  So in the absence of it, you perhaps feel compelled to use polymorphism as a substitute and combine that with various programming constructs to try to achieve your design goal (toward a hashmap facility).  But note such constructs can appear as too clever for someone else looking at your code.

Now, when 'y' does not have the pointer attribute and you seek "x%wptr_ => x%w_", note the way you are trying with defined assignment, in the client code (what you show with 'a'), the actual argument corresponding to 'x' will also need to have a TARGET attribute, if I understand the standard correctly.  But there is no way to enforce it.  This is a language limitation due to which you may want to reconsider your approach, if you are so willing.

 

元の投稿で解決策を見る

6 返答(返信)
Steve_Lionel
名誉コントリビューター III
2,595件の閲覧回数

As the error says, you can't pass a class(*) thing (a) to a non-class(*) thing (lhs). What is eq supposed to do if a isn't type w or an extension?

target
ビギナー
2,590件の閲覧回数

when x, y are both type(w), and write x = y

the assignment make sure that if y is a pointer(which means allocated(y%w_)=false), x is a pointer and x%wptr_ => y%wptr_; if y is not a pointer, x is also not and x%wptr_ => x%w_(the native assignment can't do that) 

 

as i understand, you mean: a =u calls eq(a, u), but the compiler doesn't convert [a] to class(w) like [select type], right?

FortranFan
名誉コントリビューター III
2,563件の閲覧回数
@target writes:
.. when x, y are both type(w), and write x = y

the assignment make sure that if y is a pointer(which means allocated(y%w_)=false), x is a pointer and x%wptr_ => y%wptr_; if y is not a pointer, x is also not and x%wptr_ => x%w_(the native assignment can't do that) ..

@target,

You appear to be interested in proper Generics facility in Fortran which it does not provide.  So in the absence of it, you perhaps feel compelled to use polymorphism as a substitute and combine that with various programming constructs to try to achieve your design goal (toward a hashmap facility).  But note such constructs can appear as too clever for someone else looking at your code.

Now, when 'y' does not have the pointer attribute and you seek "x%wptr_ => x%w_", note the way you are trying with defined assignment, in the client code (what you show with 'a'), the actual argument corresponding to 'x' will also need to have a TARGET attribute, if I understand the standard correctly.  But there is no way to enforce it.  This is a language limitation due to which you may want to reconsider your approach, if you are so willing.

 

target
ビギナー
2,585件の閲覧回数

still thanks, i think i find the solution

allocate(a, mold=u)
select type(a)
type is(w)
	a = u
end select
FortranFan
名誉コントリビューター III
2,592件の閲覧回数

@target writes:
.. i wanna make a flexible character type(w), which can be used as either allocatable or pointer, and can be stored in an hashmap with val=[class(*),allocatable], however the assignment(=) procedure stop me.

the error message at line(34) : error #8769: If the actual argument is unlimited polymorphic, the corresponding dummy argument must also be unlimited polymorphic. [A]

what can i do ?

 

@target,

Given what you show, you should not need the generic interface to type-bound procedure toward assignment but instead try

allocate ( a, source=u )

 

target
ビギナー
2,587件の閲覧回数

i tried, this leads to a%wptr_ => u%w_

acctually i want a%w_ = u%w_, and a%wptr_ => a%wptr_, if u%w_ is allocated

返信