magenta
magenta

Reputation: 55

Is there an effective difference between STRUCTURE and TYPE in Fortran?

My question is pretty simple. I have inherited some old Fortran code and I am looking through it to get a general feel for it. I noticed that it uses the STRUCTURE instead of TYPE. Is there any practical difference between the two? Are there any other benefits to using TYPE other than being more up-to-standard?

This question is of a more philosphical kind. I (think I) know what rewriting code means in terms of testing etc, so I am not looking for that "is it really worth it" kind of answer. More of a technical one.

I read up a bit on STRUCTURE and TYPE, the first on the interwebs, the second in "Modern Fortran explained", but I did not find more information other than "this is how it was done before Fortran 90, this is how we do it today".

Upvotes: 1

Views: 707

Answers (2)

Steve Lionel
Steve Lionel

Reputation: 7277

STRUCTURE/RECORD is a DEC Fortran extension from the mid 1980s, predating the Fortran 90 standard and derived types. In general terms, the two differ in one major way - STRUCTUREs can have UNIONs, standard Fortran has no such concept.

How many newer standard features you can use in STRUCTUREs depends on the particular compiler. The DEC-heritage compilers (Compaq and Intel) pretty much treat these as equivalents, with some minor exceptions. (You can't say SEQUENCE in a STRUCTURE, for example.) Other compilers may be more restrictive.

My advice is to use the standard derived type syntax unless you must have unions, and then recognize your code's portability is minimized.

Upvotes: 2

"but I did not find more information other than 'this is how it was done before Fortran 90, this is how we do it today'"

Well that is because it really is like that. There is not too much besides that in Fortran 90.

Of course, all the advances of Fortran 2003 and later are only guaranteed for the standard types (but any compiler can do as it pleases with structures). That means allocatable (or even pointer) components, type extension and polymorphism (class), tybe bound procedures, finalization, parametrized derived types, user defined I/O...

All this are available in the standard only for standard types. The standard does not mention the nonstandard structures at all. I am not sure if some compiler allows pointer components (Fortran 90 feature) or allocatable components (F2003) in its structures and I do not care too much. It should be in the manual.

Upvotes: 2

Related Questions