- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In F2003 (not yet IVF) there is overiding for operators and assignment using type bound procedures but how about intrinsics?
When we have a type bound precedure we must call using % notation as below. But this will mean we do not overide the intrinsic. Or am I wrong?
MODULE x
TYPE y
REAL z
CONTAINS
PROCEDURE abs => ABS_a
END TYPE
CONTAINS
CLASS(y) FUNCTION ABS_a(me)
CLASS(y), INTENT(IN) :: me
ABS_a%z = ABS(me.z)
END FUNCTION
END MODULE
PROGRAM TEST
TYPE(y) a, b
a%z = -1.0
b = abs(a) !Illegal use of intrinsic abs
b = a%abs !Will use type bound procedure
END PROGRAM
When we have a type bound precedure we must call using % notation as below. But this will mean we do not overide the intrinsic. Or am I wrong?
MODULE x
TYPE y
REAL z
CONTAINS
PROCEDURE abs => ABS_a
END TYPE
CONTAINS
CLASS(y) FUNCTION ABS_a(me)
CLASS(y), INTENT(IN) :: me
ABS_a%z = ABS(me.z)
END FUNCTION
END MODULE
PROGRAM TEST
TYPE(y) a, b
a%z = -1.0
b = abs(a) !Illegal use of intrinsic abs
b = a%abs !Will use type bound procedure
END PROGRAM
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I believe you are right. To actually "override" the intrinsic you would need to provide the usual generic interface. The specific procedure would be selected on the basis of the declared type of the argument. This specific procedure could then forward the call to the appropriate type bound procedure.
[fortran]MODULE x TYPE y REAL z CONTAINS PROCEDURE :: abs => ABS_a END TYPE ! So the intrinsic behaves like we'd expect. INTERFACE Abs PROCEDURE abs_forwarder END INTERFACE Abs CONTAINS ! gets called when dynamic type of me is a y FUNCTION ABS_a(me) RESULT CLASS(y), INTENT(IN) :: me CLASS(y), ALLOCATABLE :: r !**** ALLOCATE(y:: r) r%z = ABS(me%z) END FUNCTION ! gets called when declared type of me is a y FUNCTION abs_forwarder(me) RESULT CLASS(y), INTENT(IN) :: me CLASS(y), ALLOCATABLE :: r !**** ! polymorphic call to abs, which might go to ABS_a or equivalent override ! in an extension of y. F2008 allows assignment here? ALLOCATE(r, SOURCE=me%abs) END FUNCTION abs_forwarder END MODULE x [/fortran]But don't be surprised if I'm totally wrong :).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why the allocate statements? Isn't the variable r that holds the function value already allocated?
Regards,
Arjen
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
module module_x
implicit none
type, public:: y
real:: z
contains
procedure, pass:: abs => abs_a
end type
contains
type(y) function abs_a(me)
implicit none
class(y), intent(in) :: me
abs_a % z = abs(me % z)
return
end function
end module
program test
use module_x
implicit none
type(y):: a, b
a % z = -1.0
b = abs_a(a)
b = a % abs() ! type bound procedure
stop
end program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It seams to me that it would have made sence if F2003 or F2008 said that intrinsic procedures should be callable as type bound procedures too, then we could all adopt the new % notation for calling stuff.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page