Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OO programming complications in FORTRAN 2003

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
DE
Hi,

I have some complications in OO programing, which I do not see in c++.
Does anyone have any solution to this:

Code:
TYPE A
CLASS(B)  :: b
END TYPE A

TYPE B
CLASS(A) :: a
END TYPE B

This is not allowed in principle, because B has to be defined before it is used in the definition of A.
Because of that limitation, to implement the example above, one has to define a third type C from which B will be extended:

Code:
TYPE C
END TYPE C

TYPE A
CLASS(C)  :: b
END TYPE A

TYPE, EXTENDS(C) ::  B
CLASS(A) :: a
END TYPE B

This is not so bad, but it adds an unnecessary layer of complexity.
Does anyone have any other solution for this in FORTRAN?
The other layer of unnecessary complexity appears when we want “type­bound procedures”
(class methods) that take arguments of other types. Say we want to call AA%func(BB) and
BB%func(AA). This is how it’s done:

Code:
MODULE newTypes

TYPE AA
CONTAINS
PROCEDURE :: func => funcA
END TYPE AA

TYPE BB
CONTAINS
PROCEDURE :: func => funcB
END TYPE BB

CONTAINS

SUBROUTINE funcA(this,B)
CLASS(AA) :: this
TYPE(BB)  :: B
END SUBROUTINE funcA

SUBROUTINE funcB(this,A)
CLASS(BB) :: this
TYPE(AA)  :: A
END SUBROUTINE funcB

END MODULE

This should work. But if the types AA and BB grow big, you may want to split them in separate modules.
And this will fail, because the type­bound procedures need to have their interfaces known at compile time.
So one module will have to be compiled before the other and it will violate the non­circular dependency between module that FORTRAN wants to enforce.

The only solution I think is to have an “interface” module.

Does anyone have any better solution???
 
The standard way around mutual recursion is to use interfaces. The problem is that they way you've defined in won't work in any language: the compiler will just run out of space. You need pointers to the classes or the types.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top