Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29283 Discussions

Changing the type of a formal parameter at run time

dnoack
Beginner
1,057 Views
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
0 Kudos
8 Replies
Arjen_Markus
Honored Contributor II
1,057 Views

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

0 Kudos
dnoack
Beginner
1,057 Views
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

0 Kudos
Arjen_Markus
Honored Contributor II
1,057 Views
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
0 Kudos
dnoack
Beginner
1,057 Views
Unfortunately ifort 10.1 also does not know the extends() option.

Have you another hint, emulating polymorphism?

Regards,
dnoack
0 Kudos
Arjen_Markus
Honored Contributor II
1,057 Views
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

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

0 Kudos
Steven_L_Intel1
Employee
1,057 Views
Or you could use a more current compiler...
0 Kudos
dnoack
Beginner
1,057 Views
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
0 Kudos
Steven_L_Intel1
Employee
1,057 Views
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.
0 Kudos
Reply