Variables - Jamiras/RATools GitHub Wiki

Variables allow storage of values for for later use. They may be assigned constant or calculated values.

Variables within a function (including function parameters) are only accessible within the function. Variables outside a function may be used inside or outside of functions.

Variables may not be referenced before they are defined.

Assignment

  • name = value

name must start with a letter or underscore and may contain additional letters, numbers or underscores.

value may be a numerical constant, string constant, or expression. The expression will not be evaluated until the variable is used, which allows variables to be assigned to function calls, memory accessors, or logical comparisons to be evaluated later.

Arrays

  • name = [ value1, value2 ]

An array is a variable that stores multiple values. Arrays are accessed using square brackets. Indices are zero-based, so to access the first item of the array, use name[0]. To modify the array, use array_push and array_pop.

Example

my_array = [1, 3, 8, 15]
array_push(my_array, 21)

for i in my_array
    do_something(i)

This creates an array with four elements, then adds another element. Later, the array is iterated and a helper function is called five times (once for each element in the array).

Dictionaries

  • name = { key: value, key: value }

A dictionary is a special type of variable that maps integer values to other values - typically string constants. Anything that can be assigned to a variable can be stored in the value of a dictionary entry.

key must be a numerical or string constant or a variable or function call that evaluates to a numerical or string constant. It will be evaluated when the dictionary is being built.

Dictionaries can be accessed or modified using square brackets: value = name[key]. name[key] = value

Example

two = 2
my_dict = {
   1: "One",
   two: "Two"
}

my_dict[3] = "Three"

for key in my_dict:
   do_something(key, my_dict[key])

This creates a dictionary with two entries, then adds another entry. Later, the dictionary is iterated and a helper function is called three times (once for each entry in the dictionary).

Classes

  • name = ClassName(parameter1, parameter2)

A class is a collection of variables and functions that can access those variables.

Before a class instance can be constructed, the class must be defined.

Any variables defined within the class become fields of the class with values unique to each class instance. Any functions defined within the class have access to the class instance variables and functions through the this. keyword. Things outside of the class have access to the class instance variables and functions through the class instance.

class ClassName
{
    parameter1 = 2
    parameter2 = 4

    function func(n) => this.parameter2 * n
}

t1 = ClassName()                    // new instance with default values
t2 = ClassName(3, 9)                // new instance with specific values (parameters must be in the same order as variables in the definition)
t3 = ClassName(parameter2 = 11)     // new instance with default parameter1 but custom parameter2

x = t1.func(6)                      // x = 24 (4 * 6)
y = t2.func(6)                      // y = 54 (9 * 6)
z = t3.func(6)                      // z = 66 (11 * 6)

a = t1.parameter1 + t2.parameter1   // a = 5 (2 + 3)

Static methods and inheritance are not currently supported.