Notice: Stat is currently in private beta. This documentation is incomplete and subject to change.
Stat Docs
Variables in Stat
In stat, everything... all values, functions, and streams are all stored in variables.
You can import variables, get variables passed into functions as arguments, and you can define
variables using the
let keyword. Logically, there is no difference between a variable
that was defined, versus a variable that was imported, or a variable that was passed into
a function as an argument. Stat can't tell the difference between any of these variable types.
There is no special treatment or hidden functionality that one variable has versus another.
A variable is a variable is a variable.
All variables have an associated data type and that data type cannot be changed.
Once a variable is defined, it must always contain a value of a compatible data type.
You can change the value stored in the variable, but you can't change the type of
value(s) that the variable can hold.
Defining Variables
In Stat, you use the
let keyword to define a variable. The syntax is the let keyword, followed by the variable name,
followed by an optional type hint, followed by the equal sign, followed by the initial value. Once a variable is defined, it can
be reassigned as many times as needed, but the data type cannot change. You can't define a string, then assign an integer to that variable later on. Here are some examples: MAIN let name = "John Doe" name = "Jane Doe" let age = 22 // Using a type hint let nickName: string = "Janet"
Some things to note:
- You can only declare a single variable with the
letkeyword. - To define multiple variables, use the
letkeyword multiple times. - All variables must have an initial value when defined
- Variable names must start with a lower case letter and can only contain letters and numbers
- Using a type hint is optional in most cases, but if provided, the initial value must be compatible with the type hint (more on this later)
- All though all values are immutable (more on this later), there is no such thing as a constant variable in Stat. All variables can be reassigned
Naming variables
Variable names have strict rules. They must start with a lower case letter
a-z and they can only contain letters and numbers.
You cannot use the underscore, the dash character, or any other character like unicode characters to name your variables. MAIN // These are both invalid variable names let my_varInvalid variable name = 10 let MyVarInvalid variable name = 20
However, unlike other programming languages, there are no restrictions on what your variable names can be.
Although it's not recommended because it may confuse developers, you could name your variable say...
if or function or even letMAIN // These are all valid variable names let if = 10 let function = 20 let let = 30
Importing Variables
You can import variables by including an
IMPORTS section in your code.
Simply put all your imports in that section and a variable will be automatically created
for each import. Just like any other variable, the naming restrictions still apply to imports. IMPORTS // This creates 3 variables, one for each import myVar yourVar anotherVar
Passing variables into functions
You can define arguments in your function files and these will automatically
create variables in your function when it's called.
META type function // This creates 3 variables when the // function is called, one for each argument arg name: string arg age: int arg enabled: bool
Global Variables
There is no such thing as a global variable in Stat. The closest thing you can get
to a global variable is a function property. Function properties are set when a
program starts and retain their value throughout the life of a program. And since
you can import a function into any file, you also have access to function properties.
Variable Scope
Variable scope is simple. All variables are local to the scope that they are defined in.
Take into consideration that
if statements, while loops, for loops, run statements, and on statements all have their own scope and
any variables that are defined within these scopes aren't available outside the scope. Here are
some examples: MAIN // This variable is available anywhere in this function let term = open console if term.opened // This variable is only available within this if block let name = "John" // This is an error because the name variable isn't defined in the outer scope nameUndefined Variable: name -> term
Previous
Next