Code Design using Fortran OOP features!

Code Design using Fortran OOP features!

Hello All,

I have a question about designing a simple code using Fortran OOP features. Lets say I want a  Class "A" which has method "A1", however, I may have 3 different mplementations of A1. My question is how should I make class A to be able to run all three implementations but I want to choose among them at the compile them and not the run time (Due to code efficiency concerns). 

One method is to define three different extensions of A which each has an implementations. Isnt this a dumb approach? how if the number of implementations are so large that I could not do that without making a mess?

The other method might be using sth like template in C++ if Intel has implented this in their compiler yet!. 

I do really appreciate your input for me as a rookie programmer.

Thank you very much

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Fortran doesn't have a templating mechanism (yet) which is a limitation for this type of code. I think there was a proposal for 2008 but it was dropped - no doubt others here can give more information.

You can write three implementations as you suggest but I don't think it is particularly dumb, in C++ you would have to do that anyway in general - templates just allow savings in simple cases. You could also use an unlimitied polymorphic variable (class(*)) and write the implementation differences inside a select type - ok if the differences aren't great. A third option is to use the c-preprocessor to do the work. The particular choice will depend on what you're implementing.

You might look at PyF95 Blockit (

You can use it like a preprocessor to add templates to your Fortran code.

Jim Dempsey

Thank you for your answers,

The problem with multiple implementation is that, then how do I instantiate an object with different implemntations at compile time and not at run time (as with my understanding, choosing the implenetation to use through polymorphic variables and pointers makes the code slower).

The problem gets worst if you have containments of several objects (each with several implementations) in an object and you want to have the ability to instatiate your object with any combinations of different implementations of the contained objects  !!! 

Does anyone has a comment on the methods presented in

The second method looks like a potential to me but its not very clear again that how do I use it with different implenetations of a procedure? any input from you guys who are expert in programming?

I am sorry if I look ignorant in this area as I recently entered the object oriented progrmming area. 


Do I need to know Pyton to use pyf95++ as you introduced it to me in your previous post?

Thank you very much

In a recent thread on this, it turned out that the OP's concerns (I think it was you?) about the impact of F2003's OOP features on execution speed were not justified - for their specific example at least.

Are you sure there is a code efficiency problem?


Yes, I presented that question and based on the testings with that test code, the efficiency with the polymorphic pointer was not a problem.

However, I am looking for some less complicated approach if there is any, as to me it gets 1- complicated 2- tedious to implement a class which contains sevaral other classes where each of those classes have some methods which they can be implemented with different formula. To do so, one should make a long and complicated list of extensions of those classes and then there would be several polymorphic pointers at different levels. Please see a new thread i just made this afternoon on this subject named "Help with the design of a code with object containment !".

I really think this might be a question for many who want to use fortran2003+ features in their code design.

Leave a Comment

Please sign in to add a comment. Not a member? Join today