jski <
john.chl...@gmail.com> wrote:
> No thanks. Every language has warts (C++'ers revel/delight in its
> warts). I would simply say that as a general design philosophy,
> explicit is better than implicit.
..
> I just wanted to make sure it wasn't something peculiar to gfortran.
> I guess you're correct, I'm use to the C/C++ way of thinking.
I personally also consider the initialization-implies-save rule to be a
bit of a wart in Fortran, though it does have it's reasons. The reasons
are historical, though, so one doesn't really see them just looking at
the current state of things.
For the current state of things, mostly consider that to set something
at the beginning of a procedure, use the assignment statement. As I
think it was DIck that mentioned, the assignment statement does that
just fine and there was considered no reason to introduce new syntax to
do the same thing. I'm aware that some people have advocated for such a
new syntax, but that has not gotten enough support to happen, in my
opinion because it is just a stylistic preference, which puts it pretty
low on the priority list of things to change the language for.
For things that you want initialized once and then saved, use an
initializer and make the SAVE explicit. As for having an initializer
without an explicit SAVE, well don't do that. It just invites confusion.
If you wonder about the alluded-to historical reasons, they really go
back to Fortran 66. In Fortran 66, there was no SAVE statement. The only
standard-conforming way to make variables retain values was to put them
in COMMON and also put that COMMON in the main program (or other
suitable scope that would be active for as long as needed). But
1. That would hav ebeen a bother.
2. Pretty much nobody paid much attention to the standard in f66 days.
I'm sure there were some people who did, but they were a tiny minority.
Most people never even saw or heard of the standard and they didn't
worry about portability.
The result of 1+2 was that there was an awful lot of code that assumed
variables would retain their values between calls. That assumption was
nonstandard, but happened to work on particular machines - or even on
most machines. F77 introduced the SAVE statement, which made it much
easier to do it right, but there were still an awful lot of people who
didn't bother because the odk ways still happened to work on most
machines... and old habits are hard to break. Awfully hard. To this day,
you still find people who think that the standard actually specifies
that all variables are SAVEd. I'd have to go look for the last time I
had to correct someone on that, but I know it hasn't been long. It never
was true in terms of the standard.
So there was a lot of "f77" code that was nonstandard, but happened to
work on most systems. A very lot of code. As in probably the majority of
allegedly f77 code. I'd grant that it might possibly have not been an
absolute majority, but it was still an awful lot.
Then came f90, which essentially could not be reasonably implemented
with everything in static memory locations. Some things could still be
static, but others could not. That old nonstandard code that happened to
work on most compilers now would have started running into a lot more
compilers where it would fail.
The initialization-implies-save rule "saved" (yes, I know, groan) a lot
of those nonstandard programs by making them actually
standard-conforming in f90. So they still work - even for compilers that
tend to make many things dynamic. That rule doesn't catch all cases of
code that assumed variables values were retained, but it catches enough
of them that it was deemed worthwhile.
I personally would not have favored the rule, but then it was done
before I was on the committee anyway.