Check out Grant Acedrex, our featured variant for April, 2024.


[ Help | Earliest Comments | Latest Comments ]
[ List All Subjects of Discussion | Create New Subject of Discussion ]
[ List Earliest Comments Only For Pages | Games | Rated Pages | Rated Games | Subjects of Discussion ]

Single Comment

Game Courier Developer's Guide. Learn how to design and program Chess variants for Game Courier.[All Comments] [Add Comment or Rating]
🕸📝Fergus Duniho wrote on Mon, May 25, 2020 12:46 PM UTC:

There are two ways of getting the value of a variable. One is to prepend the variable name with the # symbol. This works by replacing the #var_name with its value during preprocessing. This is useful for commands that do not provide any evaluation of expressions. It can be used on any line, regardless of the command.

The other way works only in expressions, and it is to precede the variable name with the keyword var. In some instances, it doesn't matter which you use. But in function definitions it does. Consider these lines of code:

def test1 join "a" #v;
def test2 join "b" var v;
print fn test1;
print fn test2;
set v x;
def test3 join "c" #v;
print fn test1;
print fn test2;
print fn test3;
set v y;
print fn test1;
print fn test2;
print fn test3;

Here is their output:

a#v

b

a#v

bx

cx

a#v

by

cx

As you can tell from examining the output, the method of prepending a variable name with # works only when a function is defined, and it will insert the current value of that variable into the function definition without enabling the function to use the most recent value of the variable on subsequent function calls. But the var keyword will always retrieve the current value of the variable everytime the function is called. For this reason, it is important to use the var keyword in function definitions whenever there is any chance that its value may change. The preprocessing method of retrieving variable values may be used in a function definition only for variables that have already been set and will not change.

The same should apply to prepending constant names with @ or prepending system variables with $. You're probably okay prepending a constant name with @, because its value is not supposed to change. Some system variables change regularly, and some do not. For those that do change, it is better to use the system keyword to retrieve their value in function definitions.