Hi,
I investigating ways to modernize a fortran 90/95 code and replace a lot of derived types / operator overloading for tensor algebra with abstract classes.
My main issue deals wit how to add a method that returns an instance of an abstract class in an abstract class itself (think of overloading ‘+’ for all classes that extend an abstract class).
I found an old example on stackoverflow: fortran - How do I overload an operator for a derived type which extends an abstract type? - Stack Overflow
I understand the logic for the nested select type, but the result is ugly, needlessly verbose, and possibly inefficient… Is this really the way?
1 Like
With operator overloading, you will be disappointed to know the answer of your question is yes and that working it all out with abstract
procedures which have to be overridden with concrete types is indeed verbose that will require a lot of “boilerplate” like code with considerable complexity and inefficiency.
TL;DR: avoid operator overloading with your abstract
“classes”. Implement the operators directly with the concrete subclasses of the abstract
ones, not quite elegant but will save you a lot of trouble.
That’s what I feared. TBH, it looks like the fortran90 implementation using module, derived type and generic interface is still the way to go for what I go. That is too bad, though.
Current Fortran standard - Fortran 2018 which also includes other “goodies” from the revisions of 2008, 2003, and 95 - makes the overall experience of authoring codes toward such algebra better than it would have been with strict Fortran 90.
From a library design point-of-view, a hybrid approach involving some limited OO design but minimizing or avoiding run-time polymorphism (a la Fortran 90 since it did not have it) can be the way to go.
1 Like