2013-12-17 09:39:26 -08:00
|
|
|
^title Variables
|
2014-04-14 21:23:46 -07:00
|
|
|
^category language
|
2013-12-17 09:39:26 -08:00
|
|
|
|
|
|
|
|
Variables are named slots for storing values. You can define a new variable in
|
|
|
|
|
Wren using a `var` statement, like so:
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var a = 1 + 2
|
|
|
|
|
|
|
|
|
|
This creates a new variable `a` in the current scope and initializes it with
|
|
|
|
|
the result of the expression following the `=`. Once a variable has been
|
|
|
|
|
defined, it can be accessed by name as you would expect.
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var animal = "Slow Loris"
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(animal) // Prints "Slow Loris".
|
2013-12-17 09:39:26 -08:00
|
|
|
|
|
|
|
|
## Scope
|
|
|
|
|
|
|
|
|
|
Wren has true block scope: a variable exists from the point where it is
|
|
|
|
|
defined until the end of the block where that definition appears.
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
{
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a) // ERROR! a doesn't exist yet.
|
2014-02-16 19:27:11 -06:00
|
|
|
var a = 123
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a) // "123"
|
2013-12-17 09:39:26 -08:00
|
|
|
}
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a) // ERROR! a doesn't exist anymore.
|
2013-12-17 09:39:26 -08:00
|
|
|
|
|
|
|
|
Variables defined at the top level of a script are *global*. All other variables
|
|
|
|
|
are *local*. Declaring a variable in an inner scope with the same name as an
|
|
|
|
|
outer one is called *shadowing* and is not an error (although it's not
|
|
|
|
|
something you likely intend to do much).
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var a = "outer"
|
|
|
|
|
{
|
2014-02-16 19:27:11 -06:00
|
|
|
var a = "inner"
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a) // Prints "inner".
|
2013-12-17 09:39:26 -08:00
|
|
|
}
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a) // Prints "outer".
|
2013-12-17 09:39:26 -08:00
|
|
|
|
|
|
|
|
Declaring a variable with the same name in the *same* scope *is* an error.
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var a = "hi"
|
|
|
|
|
var a = "again" // ERROR!
|
|
|
|
|
|
|
|
|
|
## Assignment
|
|
|
|
|
|
|
|
|
|
After a variable has been declared, you can assign to it using `=`:
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var a = 123
|
|
|
|
|
a = 234
|
|
|
|
|
|
|
|
|
|
An assignment walks up the scope stack to find where the named variable is
|
|
|
|
|
declared. It's an error to assign to a variable that isn't defined. Wren
|
|
|
|
|
doesn't roll with implicit variable definition.
|
|
|
|
|
|
|
|
|
|
When used in a larger expression, an assignment expression evaluates to the
|
|
|
|
|
assigned value.
|
|
|
|
|
|
2014-01-20 21:44:51 -08:00
|
|
|
:::dart
|
2013-12-17 09:39:26 -08:00
|
|
|
var a = "before"
|
2014-04-06 08:38:00 -07:00
|
|
|
IO.print(a = "after") // Prints "after".
|