LoginLogin

Initializing variables with the VAR() function?

Root / Programming Questions / [.]

computableeCreated:
Say I have a variable name stored in string S$ (S$="VARNAME%") and I'm in an OPTION STRICT program. Is there a way to initialize VARNAME% using string S$ as a reference? I'm trying to change the way objects work in N# to eliminate several bugs and improve performance, but I can't quite figure this out. I've tried several things including
VAR VAR(S$)
to putting VARNAME% as a string literal to even see if I could initialize it in the first place
VAR "VARNAME%"
but so far, nothing has worked. Syntax errors. :/ If only the VAR function was actually documented...

Even when OPTION STRICT is not specified, using the function version of VAR gives an undefined variable error. It seems like your best bet would be a lookup table, since you would also be avoiding messing with variables that your program uses.
DIM VARLOOKUP$[0]
DIM VARMETA[0] `0 INT, -1 FLOAT, -2 STRING, >0 IF OBJECT WITH VARS;

DIM VAL%[0]
DIM VAL#[0]
DIM VAL$[0]
With this method, when you encounter a new variable name, you simply push the name of it to the table and to the array of its type. The second dimension of the VARMETA array can be used to save information such as whether it is a normal variable or. If the variable is an object with variables of its own, you can simply use positive values to specify how many need to be searched for the referenced name. If the variable loses scope in the program you can use some index shuffling to pop the variable/object.

Okay. Umm...is there a way to EXEC/USE a program slot without resetting the memory for that slot?

VAR() doesn't create variable references, it simply looks at the current variable table and gets the reference to any variable that exists with that name. The reason this doesn't work outside of STRICT mode is due to behavior we cannot directly observe. It appears to me that when not in strict mode, any variable referenced in the code will be given a VAR declaration automatically if it doesn't have one already. However, this will only work for static references; a VAR() reference doesn't count because it could technically be anything. STRICT is just a compiler setting; it throws undeclared variables instead of automatically declaring them.

Okay, interesting. How about this: Is there a way to differentiate arrays and non-arrays?

Didn't someone post a way to do this in the chat?

Didn't someone post a way to do this in the chat?
No. We never found a way. I DID, however, manage to find a way to accomplish what I wanted to without having to test for array.