In my previous post, I described getting re-acquainted with Fortran after 38 years and discovering that the language had really added some nice features over that time. I also mentioned how my “curly brace bigotry” was starting to thaw out. Well, now let’s talk about what I’ve learned over the past week.

First things first, like a good OpenBSD user, I found the mailing list for the GNU Fortran compiler and sent a question to it. Wrong! That list is for people working on the compiler itself, not for dumb n00b questions like mine. Oh whelp. They were pretty friendly about pointing me to the last place on earth I’d ever expect to be going – usenet.

For those of you who aren’t greybeards like me and have no idea what usenet is, I’ll enlighten you. Back in the dark ages of the internet, when we used dial-up access, bandwidth was very dear to everyone – even the servers on the Internet itself. Usenet was essentially a de-centralized message board that you could download information from in phases. First, you pulled down the list of boards themselves, then when you selected your board, it pulled down the most recent topics, then when you clicked on a topic, you would get the actual text of the topic and any replies to it.

The topics were arranged hierarchically like comp.os.bsd and such. There was a creepy part under the “alt” heading that had such jewels as “alt.wil.wheaton.die.die.die” for people who didn’t like the character he played on Star Trek TNG. At one point, I could swear remembering that usenet went away and was really nothing more than an archive that some sites saved. Apparently I was wrong because my friends on the compiler mailing list directed me to comp.lang.fortran. On a side note, one of the folks on that list was very kind and helped kick start some of my early learning.

After a brief internet search, I found a link and clicked on it. I don’t know if I should be seeking professional help or if others have dreams like this, but occasionally I have a “neglect dream” where (in the dream) I suddenly remember something I was supposed to have been doing but wasn’t – like feeding my non-existent giraffe. Well, the feeling I have in the dream when I head out to where the giraffe lives (expecting at the least a thin and angry horned long-neck horse) was the feeling I had clicking on that link. However, instead of a dead or murderous giraffe, I found a healthy message board that used Google Groups.

In addition to posting questions when I got stuck and reading code on the Internet, I also downloaded and read (yes – cover to cover) a Fortran programming book called “Introduction to Programming with Fortran: Third Edition” on my Kindle web app so I could read it in my browser on OpenBSD. After perusing this information, I was even more impressed with what Fortran has grown up into. One of the folks on the comp.lang.fortran group pointed out that the oldest date he was aware of was October 15, 1956 (an old IBM 704 manual apparently) so we are essentially looking at Fortran’s 60th birthday in a few months. That’s a long time for something to stay this relevant.

OK. Time to dive in. My use case was to create a string class and then add a subclass to it that uppercased all strings before storing them. Pretty simple, eh? You’d think until you learn that, while Fortran has great numerical and scientific intrinsic functions and features, it really lacks character handling capabilities. No problem, the Internet to the rescue. I found a string upper-casing and lower-casing implementation that I baked into what Fortran calls a “module”. Essentially a module is a way to encapsulate types, code (functions and subroutines) and data. Modules can be in stand-alone files and can be included into another piece of code using the “use” statement.

module foo_m

...

end module foo_m

program bar

use foo_m

...

end program bar

As you can see from the above example, this is quite similar to the “require” functionality that you see in Javascript programs of the Node.js variety, the “#include” feature of C/C++ and the “using” feature in C#.

After some pointers from the kind folks on the comp.lang.fortran mailing list, I cobbled together a bit of a style guide that I tried to follow as I built my example. By the way, if you would like to see the finished product, take a look on github and you should have a fully buildable version on OpenBSD. If you want to build this on another platform, you’ll probably have to change from “egfortran” to whatever the gfortran compiler is called on your platform (most likely gfortran).

My string_utility.f90 file ended up with the following structure:

module string_utility

implicit none

private

public :: ucase

public :: lcase

contains

pure function ucase(in_string) result(out_string)

character (len=*), intent(in) :: in_string

character(len(in_string) :: out_string

...

end function ucase

pure function lcase(in_string) result(out_string)

...

end function lcase

Some things to point out here. The use of “implicit none” brought back some fond memories. FORTRAN used to automatically type declared variables in a really cute way. If the variable name started with a letter between ‘I’ and ‘N’, it assumed it was an INteger. Ha! Since we want to declaratively type things these days and not have “odd” side-effects from features like that, implicit none turns this feature off for the module.

Fans of other OO languages like C++, Java, etc. will recognize the use of “public” and “private”. Fortran is public by default, so putting the private declaration at the top of the module flips this to the way I feel most comfortable.

The “pure” keyword isn’t really needed perhaps, but I threw it in to illustrate that feature of the language. Fortran is very focused on parallel programming and this feature is essentially a compiler hint that the code in that procedure is deterministic and can be ran in parallel if necessary. For a better description than a n00b like I can produce, take a look at this stack overflow post.

The last interesting bit in this code snippet is how I declared the return type from my function. You can do it two ways, the traditional supply-the-type-before-the-name way (in which case you specify the return value by assigning your result to the name of the function in its body) or the way I did it here where I have a specific variable I declare to hold the result. No preference, this just looks tidy to me.

By the way, it is probably worth pointing out that, unlike the C languages and their offspring, Fortran has two types of procedures – functions and subroutines. A function returns a value and requires no special calling syntax and a subroutine returns no value but requires the use of the call subroutine_name() syntax to invoke.

The next thing we’ll talk about is how you build the equivalent of a class from the curly-brace languages. That is done in Fortran by creating a type in a module. Take a look at the following code skeleton:

module string_m

implicit none

private

type, public :: string_t

private

character(:), allocatable :: str_m

contains

private

procedure, public, pass(this) :: get_value => get_value_string_t

...

end type string_t

...

end module string_m

Ok. There are many interesting things going on in this code. In the type declaration, you see me creating a type called “string_t”. The “_t” bit is just convention, it isn’t syntactically necessary. The private variable “str_m” is an “allocatable” character array, or what I would call a “string”. You also see a public procedure called “get_value” that is aliased to a local function I called “get_value_string_t”.

Finally the “pass(this)” part is some cool magic. This is where you declare the equivalent of a “this” pointer in C++ that contains a reference to the object in who’s context your procedure is being invoked. The slick thing is you can call it whatever you want so you aren’t tied to a particular name. Plus (to me at least) the fact that you have to explicitly declare it takes away some of the “magic stuff” in C++.

To subclass this little gem, it is pretty straightforward. Take a look at the following code skeleton to see how that happens in another module:

module ustring_m

use string_m, only : string_t

use string_utility, only : ucase

implicit none

private

type, public, extends(string_t) :: ustring_t

contains

procedure, public, pass(this) :: set_value => set_uvalue

...

end type ustring_t

...

end module ustring_m

OK. Lots of stuff going on here. For one, did you notice the new “use” feature I pulled out of the air? If you say “, only :: ” and list a set of components, only those components are used from the included module. This helps you avoid unintended name space collisions from crud that happens to live in a module.

You can see from the “extends” piece of syntax, that this is how you subclass your base class. Finally, if you look at the procedure, you see that I am overriding the “set_value” procedure from the base class and mapping it to the local function that I named “set_uvalue”.

At this point, I knew enough to be dangerous and, as is my speciality, decided to soar over the tips of my skis and crash into the mountain – I had read about operator overloading and decided to create an overload of the equals operator!

Boom!

Well, that was painful. Back to the comp.lang.fortran group for some healing and education. I learned that, while you can overload operators in Fortran, what I wanted to do was overload “assignment”. After a lot of back and forth with some incredibly patient people out there, I discovered the solution that makes all of this make sense and be pretty simple to do.

You declare the assignment operator in your base class and use the ability to alias the names of procedures in your subclass to overload the actual methods performed. Here is how it looks in the base class:

type, public :: string_t

private

character(:), allocatable :: str_m

contains

private

generic, public :: assignment (=) => string_t_assign_string_t, &

string_t_assign_character

! Procedure declaration for internal methods

procedure, private, pass(lhs) :: string_t_assign_string_t, &

string_t_assign_character

end type string_t

Believe me, this looks way cleaner than the messes I created along the way to learning how to do it. Oh, and did I mention that Fortran uses the exclamation mark (bang) for comments? It feels slightly ironic to me like, “Holy crap! I’m actually putting a comment in my code! Can you believe it?” I used to work with a guy who said that anyone not smart enough to understand his code by reading it shouldn’t be in there in the first place – that’s why he never commented it. 😉

So the interesting bits here are the use of the “generic” keyword. This allows us to genericise what can be on the right-hand side of that assignment operator. Very similar to generics in other languages. Finally, notice that we have to declare the actual procedures that do the assignment (one from a string_t type and a second one from a character or character array) twice. Once in the mapping and a second time for the actual declaration.

The implementation of the procedures is pretty straightforward:

elemental subroutine string_t_assign_character(lhs, rhs)

class (string_t), intent (inout) :: lhs

character(len=*), intent (in) :: rhs

lhs%str_m = rhs

end subroutine string_t_assign_character

The only piece of magic here is the use of the keyword “class” so that you can tell the compiler you might want to allow someone to override this procedure later in a subclass. If you don’t want that, just use “type”. Finally, I picked this method to show the implementation of because of the use of “elemental”. This tells the compiler that you can pass a single character or an array of characters.

One thing that a good object needs is a constructor in C++ to initialize internal data, etc. Figuring this out in Fortran was a little challenging. What I ended up with is as follows:

module string_m

implicit none

private

type, public :: string_t

private

! Internal private character store

character(:), allocatable :: str_m

contains

private

end type str_t

! Class constructor

interface string_t

module procedure string_t_constructor

end interface string_t

contains

type (string_t) function string_t_constructor()

string_t_constructor%str_m = ""

end function string_t_constructor

So the trick here is to declare the interface, then actually define the function in the contains section of the module, returning a type of “string_t”. I’m not certain if the “_constructor” is convention or required.

Now I can build a little test program to exercise my classes like this:

program fortranString

! Pulls in public interface for our String module

use string_m, only : string_t

use ustring_m, only : ustring_t

! Prevent default I-N integer assumption

implicit none

! Declare local variables

type (string_t) :: string1

type (string_t) :: string2

type (ustring_t) :: string3

string1 = "Bonjour"

print *, string1%get_value()

end program fortranString

As you can see, we can call the methods in the classes, use the assignment operator and all sorts of fun things like that!

So, in conclusion, Fortran is a pretty cool language. The syntax is a little different that a curly-brace guy like me is used to, but once you figure it out, it’s pretty easy to use and has a very nice feature set. Again, if you’d like to look at a functional complete example, check out my source repository on GitHub.

I’m going to do a third post in this series where I actually build a modern web application using Fortran for the middle tier (I’m thinking I need a cool name like LAMP or BCHS so maybe FARM – Fortran, Apache, REST and mySQL?) but that’s for another day. Hope you enjoyed reading this as much as I enjoyed learning it.