Lots of work on docs.

This commit is contained in:
Bob Nystrom
2014-01-20 21:44:51 -08:00
parent 90d1ed6aa6
commit 95d239cd21
9 changed files with 116 additions and 125 deletions
+10 -9
View File
@@ -3,36 +3,36 @@
Variables are named slots for storing values. You can define a new variable in
Wren using a `var` statement, like so:
:::wren
:::dart
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.
:::wren
:::dart
var animal = "Slow Loris"
io.write(animal) // prints "Slow Loris"
io.write(animal) // Prints "Slow Loris".
## 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.
:::wren
:::dart
{
io.write(a) // ERROR! a doesn't exist yet
io.write(a) // ERROR! a doesn't exist yet.
var a = 123
io.write(a) // "123"
}
io.write(a) // ERROR! a doesn't exist anymore
io.write(a) // ERROR! a doesn't exist anymore.
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).
:::wren
:::dart
var a = "outer"
{
var a = "inner"
@@ -42,7 +42,7 @@ something you likely intend to do much).
Declaring a variable with the same name in the *same* scope *is* an error.
:::wren
:::dart
var a = "hi"
var a = "again" // ERROR!
@@ -50,6 +50,7 @@ Declaring a variable with the same name in the *same* scope *is* an error.
After a variable has been declared, you can assign to it using `=`:
:::dart
var a = 123
a = 234
@@ -60,7 +61,7 @@ doesn't roll with implicit variable definition.
When used in a larger expression, an assignment expression evaluates to the
assigned value.
:::wren
:::dart
var a = "before"
io.write(a = "after") // Prints "after".