Function Request: Static variables

Another function request; this request is mostly syntax sugar.

We have now local Methods (method defined in a method - which can only be accessed from that method),
local Properties (property defined in a method - which can only be accessed from that method).

I miss the local Fields; a field defined in a method - which can only be accessed from that method.
It is different from a variable in such a way that the local field retains it value between calls to the method. In Visual Basic this is called a Static Variable (see https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/static).

A static variable is in essence a private variable in the class that can only be accessed from the method where it is defined.

Example as we can do it now:

type x = class
    private 
        myVar: int := 1;
    public
        method MyMethod();
end;

implementation

method x.MyMethod();
begin
    //in the first call it will print 1, in the second 2, and so on
    Console.WriteLine(myVar);
    MyVar := MyVar + 1;
end;

Example with a static variable:

type x = class
    public
        method MyMethod();
end;

implementation

method x.MyMethod();
begin
    // will initialize the variable to one in the first call, will not change it in the second, third and so on.
    Static MyVar : Int := 1/
    //in the first call it will print 1, in the second 2, and so on
    Console.WriteLine(myVar);
    MyVar := MyVar + 1;
end;

The difference between the first en second example is that in the first example the value of MyVar can be changed in any method, in the second it can only be changed in the method MyMethod.

hmm, so really it’s an instance field of the class, with its access being restricted to the one method?

feels to me that this would be more cleanly expressed as a private field declared close-by to the method itself…

I use them a lot for caching values in VB.Net.
And in essence, local methods and local properties work the same …

I think that a class definition should be as clean as possible, so when a field is only used for one method, it should be defines in that method.

Yes, but not completely.
If the method is a class method, the field is internally a class field, if it is an instance method, the field is internally an instance field.

Just like local methods and local properties, you can do without them and just define them in the class, but for good code readability and local containment you need the local versions.

Further, there is one more difference.
The field MyVar in the first example is initialized when the class is instantiated - regardless if any call to MyMethod will ever be made.
The static in the second example will be initialized on the first call to the method MyMethod.
So it gives some kind of optimization too; it prevents fields from being initialized when they are not needed. (In this example not really much because it is an int, but it can also be a complex class - in that case you really win time and memory).

@mh, @ck
I thought “Maybe I can implement it myself using aspects” …
But I can not set an attribute or aspect on a local variable :frowning_face:, so that did not work …
So I need you guys to implement it for me; unless you can tell me how and where to start.

Yeah, I don’t think you’ll be able to do that via aspects.

I can log a feature request, but it’ll be low priority, as we have a huge lot of things going on right now on the compiler side, already…

1 Like

Thanks, logged as bugs://82729