Variables in SenseTalk
What are Variables?
Variables are named containers. They serve as holders for values, similar to the “memory” function on a calculator. Unlike most simple calculators, which have only a single memory location that holds one number, you can store many values at once in different variables. Also, the value stored in a variable is not limited to a single number, but can be an arbitrarily large amount of data. To keep track of the different variables, each one has a name.
SenseTalk provides three kinds of variables:
Names of local, global, and universal variables must begin with a letter or an underscore character _
. The name can be any length and can contain letters, digits, and underscores, plus many non-punctuation special characters. All variable names are case-insensitive (upper-and lower-case letters are treated as equivalent).
Some valid variable names:
Total
_new_moon_
tree23
Ω
Some invalid variable names:
3options -- variable names can't begin with a digit
ToBe?OrNot -- variable names can't contain punctuation or spaces
Local Variables
Local variables are the most common. They exist within the context of a single handler in a script. To create a local variable, you simply put something into it in a script. For example, the command put 5 into foo
will create the local variable foo
if it doesn’t already exist and store the number 5 in it.
Local variables cannot be accessed outside of the handler (or script) where they are used. References to foo
in another handler will be to a separate foo
that is local to that other handler. Local variables are temporary: their values go away when a handler finishes executing.
Parameters declared after the handler name in a to
, on
, or function
declaration are a special type of local variable. Parameter variables are different from other local variables because when the handler begins executing they already contain values passed by the sender of the message. Other local variables have no initial value when the handler begins executing.
Local variables that have not been assigned a value (and that do not have a special predefined value, as described in Constants and Predefined Variables) will evaluate to their names. This provides a form of unquoted text string literal, which can be convenient in some instances, but might lead to confusion if you later choose to store something into a variable by that name. Generally, it is best to always use quotes around text literals to avoid any unexpected results.
Undefined Variables and the StrictVariables Global Property
Local variables that have not yet been assigned a value will ordinarily be treated as unquoted literals—in other words, their value is the same as their name:
put Hello --> "Hello"
Sometimes this behavior can lead to trouble, such as if you inadvertently misspell the name of a variable later in the script and expect it to have the value previously stored in it. To help with debugging your script in such cases, or if you simply prefer a more rigorous approach, you can set the strictVariables
global property to true. When this property is set, any attempt to access a variable that has not been explicitly declared or assigned a value will throw an exception rather than returning the variable’s name:
put Bonjour into greeting --> Stores "Bonjour" in the variable greeting
set the strictVariables to true
put Bonjour into greeting2 --> Throws an exception