- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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) ..
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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) ..
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
still thanks, i think i find the solution
allocate(a, mold=u)
select type(a)
type is(w)
a = u
end select
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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 ?
Given what you show, you should not need the generic interface to type-bound procedure toward assignment but instead try
allocate ( a, source=u )
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page