- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am having difficulty in understanding the ownership of type bound procedure names. IdeaIly, I would like to use them like the member functions of c++.
Say, I have type apple:
MODULE apple_mod
TYPE, PUBLIC :: apple
CONTAINS
PROCEDURE, PUBLIC :: get_num
END TYPE apple
CONTAINS
INTEGER FUNCTION get_num(this )
IMPLICIT NONE
CLASS ( apple ) :: this
get_num=1
RETURN
END FUNCTION get_num
END MODULE apple_mod
and a similar data type, say orange, which has a type bound procedure of the same name, but returning different value :
MODULE orange_mod
TYPE, PUBLIC :: orange
CONTAINS
PROCEDURE, PUBLIC :: get_num
END TYPE orange
CONTAINS
INTEGER FUNCTION get_num(this )
IMPLICIT NONE
CLASS ( orange ) :: this
get_num=2
RETURN
END FUNCTION get_num
END MODULE orange_mod
If I were to use both modules in the main program below, or within a module that does not contain get_num there are no problems:
PROGRAM main
USE apple_mod
USE orange_mod
IMPLICIT NONE
TYPE(apple), POINTER :: apple_ptr
TYPE(orange), POINTER :: orange_ptr
WRITE (*,*) apple_ptr%get_num(), orange_ptr%get_num()
END PROGRAM main
However, if I try to use apple in pear, which has its own get_num,
MODULE pear_mod
USE apple_mod
TYPE, PUBLIC :: pear
TYPE(apple), POINTER :: apple_ptr
CONTAINS
PROCEDURE, PUBLIC :: get_num
END TYPE pear
CONTAINS
INTEGER FUNCTION get_num(this )
IMPLICIT NONE
CLASS ( pear ) :: this
get_num=3
RETURN
END FUNCTION get_num
END MODULE pear_mod
I get the error: The name of the module procedure conflicts with a name in the encompassing scoping unit. [GET_NUM]
I know I can use aliases, say get_num => get_num_apple etc., but should this be necessary, ie who owns get_num?
apple_mod::get_num, apple::get_num or apple_mod::apple::get_num
Many thanks,
Deniz
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This isn't really related to type-bound procedures. It's that you have a module procedure named get_num and then try to define "locally" another get_num. This violates Fortran's rules for name resolution.
One solution is to uniquely name the module procedures and then specify a "binding-name" in the type declaration. Like this:
MODULE apple_mod
TYPE, PUBLIC :: apple
CONTAINS
PROCEDURE, PUBLIC :: get_num => apple_get_num
END TYPE apple
CONTAINS
INTEGER FUNCTION apple_get_num(this )
IMPLICIT NONE
CLASS ( apple ) :: this
apple_get_num=1
RETURN
END FUNCTION apple_get_num
END MODULE apple_mod
Or, you could use access control to prevent the module procedure names from being visible by use association, like this:
MODULE apple_mod
PRIVATE
TYPE, PUBLIC :: apple
CONTAINS
PROCEDURE, PUBLIC :: get_num
END TYPE apple
CONTAINS
INTEGER FUNCTION get_num(this )
IMPLICIT NONE
CLASS ( apple ) :: this
get_num=1
RETURN
END FUNCTION get_num
END MODULE apple_mod
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I had already implemented the first solution, but was really looking for the second, which works nicely. Thank you very much for returning promptly.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page