- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I want to branch to different subroutines according to the type of the formal parameter at run time. In the enclosed example I use two types t1 and t2 and the interface branch.
If I use the marked statement call branch(a) in subroutine sub, the module procedure branch1 is called and if I manually modify the statement to call branch(b) the module procedure branch2 is called. This seems correct.
But I wonder, if there is a way to change the type of the formal parameter at run time, without manually change the call branch(...) statement. Any hints are appreciated.
Thank you in advance
dnoack
I want to branch to different subroutines according to the type of the formal parameter at run time. In the enclosed example I use two types t1 and t2 and the interface branch.
If I use the marked statement call branch(a) in subroutine sub, the module procedure branch1 is called and if I manually modify the statement to call branch(b) the module procedure branch2 is called. This seems correct.
But I wonder, if there is a way to change the type of the formal parameter at run time, without manually change the call branch(...) statement. Any hints are appreciated.
Thank you in advance
dnoack
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can do that by extending t1 and t2 from a common type or by using an unlimited polymorphic
variable:
class(*), pointer :: up
up => a
call branch(up)
up => b
call branch(up)
Similarly, if I understand it all correctly, via such a common class.
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you to point me to the right way. This is correct what I want. But unfortunately my compiler (ifort 10.1 on linux platform) complains
fortcom: Error: pbranch.f, line 16: Syntax error, found ',' when expecting one of: => = . ( %
class(*), pointer :: up
--------------^
fortcom: Error: pbranch.f, line 16: Syntax error, found END-OF-STATEMENT when expecting one of: , )
class(*), pointer :: up
-----------------------------^
compilation aborted for pbranch.f (code 1)
I think it doesn't know about class(*). Which compiler I need?
Regards,
dnoack
fortcom: Error: pbranch.f, line 16: Syntax error, found ',' when expecting one of: => = . ( %
class(*), pointer :: up
--------------^
fortcom: Error: pbranch.f, line 16: Syntax error, found END-OF-STATEMENT when expecting one of: , )
class(*), pointer :: up
-----------------------------^
compilation aborted for pbranch.f (code 1)
I think it doesn't know about class(*). Which compiler I need?
Regards,
dnoack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
class(*) declares an unlimited polymorphic type, Intel Fortran 10.1 definitely does not
support that, but you may try it with a base type, like:
type base
end type
type t1, extends(base)
...
end type
type t2, extends(base)
...
end type
This ought to work, but I have not tried ;)
Regards,
Arjen
support that, but you may try it with a base type, like:
type base
end type
type t1, extends(base)
...
end type
type t2, extends(base)
...
end type
This ought to work, but I have not tried ;)
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately ifort 10.1 also does not know the extends() option.
Have you another hint, emulating polymorphism?
Regards,
dnoack
Have you another hint, emulating polymorphism?
Regards,
dnoack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What you can do then is:
type compound
type(t1), pointer:: t1_data
type(t2), pointer :: t2_data
end type
and then dispatch on the field that is associated:
if ( associated(comp%t1_data) ) then
call branch(comp%t1_data)
endif
type compound
type(t1), pointer:: t1_data
type(t2), pointer :: t2_data
end type
and then dispatch on the field that is associated:
if ( associated(comp%t1_data) ) then
call branch(comp%t1_data)
endif
if ( associated(comp%t2_data) ) then
call branch(comp%t2_data)
endif
It is not perfect, but the technique has been described by Szymanski and Decyk and others,
and it does not rely on Fortran 2003 features.
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Or you could use a more current compiler...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Getting the newest compiler is not a problem for me. But the question is, whether all addressees of my program have also the newest compiler available.
dnoack
dnoack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If your program is built into an executable or DLL, then users of your program don't need the latest compiler. If you are providing static libraries or objects, then yes they would.

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