Reputation: 946
The problem is renaming a member of base type in its derived type. I think it can be done since FORTRAN provides pointer and associate function. For example:
type :: base1
integer :: old
endtype base1
type, extends(base1) :: derived1
!here I want old to be renamed as new
!Something like new=> old
!So I can use derived1%new elsewhere
endtype derived1
But the problem is base1%old cannot be target. So I guess I cannot use pointer to do it. I have no idea where to put associate. Any suggestions? Thanks!
Upvotes: 0
Views: 316
Reputation: 21431
The language does not provide for the renaming of components inherited from parent types, in any sort of general way.
While you could conceptually associate a pointer component of the extended type to a component of the parent type in an overloaded structure constructor or similar, there are numerous pitfalls.
For the pointer association of the component of the extended type to remain valid outside of the overloaded structure constructor the object would always need to have the target attribute (or the parent component would itself need to be a pointer).
Any time the value of the object was copied (which is broader than just assignment - consider VALUE and the SOURCE= specifier in ALLOCATE) you would need to re-associate the pointer component of the extended type in the copy.
I wouldn't go down this path.
Conceptually, if the component of the parent type is public then the component is part of the interface of that parent type. Type extension implies that the extended type has a superset of the interface of the parent type, but the desired renaming of the component suggests you don't want that. If you don't want the interface of the extended type to be a superset of that of the parent type, then perhaps the extended type shouldn't be an extension of the parent at all - maybe you would be better off making what is now the parent type a normal component of the extended type.
Upvotes: 3