In my opinion, static variables should only be allowed to be declared inside functions (in a dedicated block or declared as inlined), because that is their purpose—they are meant to physically exist in memory in the same place as global variables, but their scope should be local to a function.
Declaring static variables outside of functions—for example, in the
implementation section—defeats their purpose and is no different from the declaration and purpose of a regular, global variable. So, in my opinion, if static variables are to be supported, their declaration should be restricted to inside functions only, so as not to allow a given variable to be declared in two different ways, intended for two different uses.
It would also be a good idea to consider supporting
static thread variables, as they would also be useful in multithreaded programs. In other words, not only should it be possible to declare static variables with a single instance for the entire program, but also static thread variables with a single instance for each worker.
This calls for a slightly different syntax for declaring static variables, so as to distinguish between regular and thread-specific static variables, and to align the syntax with the current Free Pascal syntax. My suggested syntax:
// declaration sections
var
Foo: Integer; // regular local variable
static var
Foo: Integer = $BEEF; // regular static variable
static threadvar
Foo: Integer = $BEEF; // static variable, one per thread worker
// function body
begin
// inlined regular local variable
var Foo: Integer;
// inlined regular static variable
static var Foo: Integer = $BEEF;
// inlined static variable, one per thread worker
static threadvar Foo: Integer = $BEEF;
{..}
end;
What do you thing about it?