How To - Declare identifier as Static Variable

Continuing the discussion from AmiBroker 6.26.0 BETA released:
My psychic skills are rusty… Can someone provide the AFL syntax for declaring a static variable and then using it without function calls (e.g. eliminating need for StaticVarTextSet() and StaticVarTextGet())? A code sample would be helpful.

CHANGES FOR VERSION 6.26.0 (as compared to 6.25.0)
AFL: new static keyword: declare identifier as static variable – a little ‘revolution’ in static variable use, declare variable as static and use as ‘regular’ variable, no need to call functions - this is Private API(1)

Psychic Skills are Back! Found this in Amibroker Readme.html

CHANGES FOR VERSION 6.27.0 (as compared to 6.26.0)

AFL: #pragma enable_static_decl accepts prefix given in quotation marks (as regular string).
#pragma enable_static_decl “myprefix{chartid}”

AFL: StaticVarRemove() when called with EMPTY string: StaticVarRemove("") nullifies all static variables declared with static keyword within given formula, so this:

#pragma enable_static_decl(test) 
static _x, _y, _z; 

_x = 1; 
_y = 2; 
_z = 3;

@Tomas - I must be doing something wrong!

I want to declare a static variable (using a prefix) and reference it with StaticVarGet() (mainly so I can use the static variable in a dynamic manner). Is this possible or is there another way to have a dynamic static variable but not have to use functions to set or get its value?

StaticVarRemove(""); StaticVarRemove("*");

#pragma enable_static_decl(myPrefix) 

SVList = StaticVarInfo("*","list");
_TRACE("AFL - SVList=" + SVList );

static _a;
_a = 1; 
_TRACE("AFL - value for _a = " + _a);

bStatic = StaticVarGet("_a");
_TRACE("AFL - value for bStatic = " + bStatic); //expected 1 returns {empty}

cStatic = StaticVarGet("myPrefix_a"); //expected 1 but returns {Empty} 
_TRACE("AFL - cStatic = " + cStatic );

The name of the declared static variable is not "_a" It is "myprefix_a". But it is minor detail. What is important is written here: AmiBroker 6.26.0 BETA released

It is important to read last comment, which explains that the content (or value) of declared static variables is read from ‘global’ memory once (when the variable is declared) and finally written to ‘global’ memory once - when your formula finishes. During your formula execution the variable keeps local copy.

So if you call StaticVarGet you will get OLD content of the declared _a until formula finishes. As I wrote in the thread that I pointed

Moral story is: DO NOT MIX / DO NOT CALL StaticVar functions on declared static variables within SAME formula, as they are operating on different memories*

1 Like

Ahhha… That now makes sense! Again, thanks!