It's great that you're discussing the proposed static/persistent variable feature - I'm happy to see where this conversation is going. Just keep one thing in mind: this would be an Unleashed feature (let's be realistic - there's no way this lands in upstream FPC).
One of the main features in Unleashed, disliked by many, are
inline vars. So if we're talking about special kinds of variables and the blocks/sections where they should be declared, it would be nice if they could also be declared inline.
From what I've read and figured out in this thread, the best approach would be to use the
static keyword in three contexts. You can't make everyone happy - we need to find a compromise. My current proposal is to allow
static in 3 places:
1. static as a declaration section (alternative to writable typed constants, which stay for backward compatibility):
procedure Foo;
static
counter: Integer = 0;
begin
Inc(counter);
end;
2. static as an attribute in a var section:procedure Foo;
var
counter: Integer = 0; static;
begin
Inc(counter);
end;
3. static as an attribute in an inline-var:procedure Foo;
begin
var counter: Integer := 1; static;
Inc(counter);
end;
Since
static works as an attribute in inline-var, there's no reason it couldn't be used in a regular
var section too - that's just a natural extension.
And since we're introducing a keyword in a new context anyway (well, an existing keyword, but used in new circumstances), why not add type inference for static vars while we're at it? For example:
// inline-var
var counter := 1; static;
// static section
static
counter := 0; // inferred as Integer
name := 'default'; // inferred as String
No need to spell out the type - the compiler figures it out from the initial value (integer literals as Int32, strings always as String even if the value is a single character)
This gives us the clean separation from
const that
@440bx is asking for, it naturally extends to inline declarations and regular
var sections - the direction Unleashed is heading anyway - and as a bonus we get a more convenient syntax with type inference.
Sure, that's 3 places where
static can appear - but as I've mentioned earlier in this thread,
it's the programmer's responsibility to keep their code readable. You can write code with inline vars that is perfectly clear to someone reading it for the first time, and for some of us inline vars actually
improve readability compared to having variables declared somewhere far away in a single block at the top (myself included). It's the programmer's choice and responsibility to write clean code. The syntax should make it possible - how people use it is up to them.
On top of that, this is great for small programs, personal unpublished projects, quick prototyping - and when it's time to publish, you can always move those variables into proper sections. Maybe even use
@Thaddy's de-inliner to do it for you
