Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28807 Discussions

the assignment(=) leads to an error #8769

target
Beginner
1,997 Views
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 Kudos
1 Solution
FortranFan
Honored Contributor III
1,953 Views
@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.

 

View solution in original post

0 Kudos
6 Replies
Steve_Lionel
Honored Contributor III
1,986 Views

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?

0 Kudos
target
Beginner
1,981 Views

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?

0 Kudos
FortranFan
Honored Contributor III
1,954 Views
@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.

 

0 Kudos
target
Beginner
1,976 Views

still thanks, i think i find the solution

allocate(a, mold=u)
select type(a)
type is(w)
	a = u
end select
0 Kudos
FortranFan
Honored Contributor III
1,983 Views

@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 )

 

0 Kudos
target
Beginner
1,978 Views

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

0 Kudos
Reply