Sentinel
Language: Variables
Variables store values that can be used later.
Most notably, a variable assignment of main
is required for all
Sentinel policies. This is the main rule that
is executed to determine the result of a policy. The main
rule may
use other variables that are assigned in the policy.
Example:
a = 1
b = a + 1
In this example, two variables are assigned: a
and b
. a
is given
the value of "1" and b
uses the a
to calculate its own value.
Syntax
The syntax for assigning a variable is:
IDENTIFIER = VALUE
On the left of the equal sign is an identifier. This is a name for your
variable and will be how you reference it later. An identifier is any
combination of letters and digits, but must start with a letter. An
underscore _
is also a valid letter.
On the right is any valid Sentinel value or expression. A value is a literal value such as a number or string. An expression is some computed value such as doing math, calling a function, etc.
Assignment and Reassignment
A variable is assigned when it is given a value. You can also reassign variables at any time by setting it to a new value. The new value takes effect for any subsequent use of that variable. A variable can be reassigned to a different type.
For example:
a = 1 // a = 1
b = a // b = 1
a = "value" // a = "value", b = 1
c = a // c = "value", b = 1
In the above example you can see that the variables are set and reassigned.
Notice that the value of a variable is the current value, and that reassigning
a variable only affects future uses of that variable. You can see this with
c
and b
being different values.
Unassigned Variables
Using a variable that is unassigned is an error.
In the example below, the first line would result in the policy erroring.
Sentinel is executed top-down and the value of c
is not available yet
on the first line.
a = c // Error!
c = 1
Type Conversion
While a topic more related to values, variables can be assigned (or-reassigned) a different value type via type conversion.
s = "1.1"
a = int(s) // a = 1
a = float(s) // a = 1.1
a = 1
s = string(a) // s = "1"
For more details, see the type conversion sections in the values page, or the Sentinel Language Specification.