Hi,
I have some complications in OO programing, which I do not see in c++.
Does anyone have any solution to this:
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:
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 “typebound 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:
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 typebound 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 noncircular 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???
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 “typebound 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 typebound 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 noncircular 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???