Skip to content

Variables

Dustin Van Tate Testa edited this page May 16, 2019 · 1 revision

This information will soon be obsolete as changes are coming to the interpreter which will make some changes to the way these things function.

Variables

Variables are named references, sometimes variables are simply called references as their functionality is equivalent. Variables are created as soon as they are named. Initially they point to empty. Variable names start with a $ as this is shorthand for using the $ operator ($a vs. 'a' $).

Assignment

Variables can be assigned using the set operator or the = operator. You must use the set operator when you don't want to assign to a variable in a previous scope. You should use the set operator when you want to initialize a variable in the current scope.

$a 5 set
{ # change $a from the previous scope's value to 6
   $a 6 = 
} @
$a println # 6


$b 5 set
{ # create a new variable $b in current scope and set it to 6
   $b 6 set
} @
$b println # 5

Scoping

YodaScript is Dynamically scoped, that is variables in the current scope depend on the program state. Ie - if you try to use a variable that hasn't been assigned yet, it will give you empty. This is best expressed via the following example. This makes it important to give variables meaningful names and prevent relying on global variables. Perhapse in a future version of YodaScript I'll switch to using a lexical scoping system.

$var 5 set # set global variable var to 5
$f {
    $var 10 set # set a local variable var to 10
    $g @ # run $g in new scope
} set
$g { 
    $var println 
} set

# run g in new scope
$g @ # prints 5

# run $f in new scope which runs $g in new scope
$f @ # prints 10

Referencing a variable from a previous scope produces a new variable in the current scope which points to the variable in the previous scope. When you use the = operator, it changes the value that the previous scope points to. And when you use the set operator it changes the value of the variable in the current scope.

Clone this wiki locally