Skip to content

Commit d4ed7c9

Browse files
committed
docs for local, fixes #64
1 parent 0759cd6 commit d4ed7c9

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

docs/reference.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ variable. You can assign multiple names and values at once just like Lua:
3131
```
3232

3333
If you wish to create a global variable it must be done using the
34-
[`export`](#export) keyword.
34+
[`export`](#export_statement) keyword.
3535

36-
The `local` keyword can be used to forward declare a variable, or shadow an
37-
existing one.
36+
The [`local`](#local_statement) keyword can be used to forward declare a
37+
variable, or shadow an existing one.
3838

3939
## Update Assignment
4040

@@ -163,7 +163,7 @@ automatically includes a `self` argument.
163163
### Argument Defaults
164164

165165
It is possible to provide default values for the arguments of a function. An
166-
argument is determined to be empty if it's value is `nil`. Any `nil` arguments
166+
argument is determined to be empty if its value is `nil`. Any `nil` arguments
167167
that have a default value will be replace before the body of the function is run.
168168

169169
```moon
@@ -1104,6 +1104,70 @@ The `export` statement can also take special symbols `*` and `^`.
11041104
current scope. `export ^` will export all proper names, names that begin with a
11051105
capital letter.
11061106

1107+
## Local Statement
1108+
1109+
Sometimes you want to declare a variable name before the first time you assign
1110+
it. The `local` statement can be used to do that.
1111+
1112+
In this example we declare the variable `a` in the outer scope so its value
1113+
can be accessed after the `if` statement. If there was no `local` statement then
1114+
`a` would only be accessible inside the `if` statement.
1115+
1116+
```moon
1117+
local a
1118+
if something
1119+
a = 1
1120+
print a
1121+
```
1122+
1123+
`local` can also be used to shadow existing variables for the rest of a scope.
1124+
1125+
```moon
1126+
x = 10
1127+
if something
1128+
local x
1129+
x = 12
1130+
print x -- prints 10
1131+
```
1132+
1133+
When you have one function that calls another, you typically order them such
1134+
that the second function can access the first. If both functions happen to
1135+
call each other, then you must forward declare the names:
1136+
1137+
```moon
1138+
local first, second
1139+
1140+
first = ->
1141+
second!
1142+
1143+
second = ->
1144+
first!
1145+
```
1146+
1147+
The same problem occurs with declaring classes and regular values too.
1148+
1149+
Because forward declaring is often better than manually ordering your assigns,
1150+
a special form of `local` is provided:
1151+
1152+
```moon
1153+
local *
1154+
1155+
first = ->
1156+
print data
1157+
second!
1158+
1159+
second = ->
1160+
first!
1161+
1162+
data = {}
1163+
```
1164+
1165+
`local *` will forward declare all names below it in the current scope.
1166+
1167+
Similarly to [`export`](#export_all_and_export_proper) one more special form is
1168+
provided, `local ^`. This will forward declare all names that begin with a
1169+
capital letter.
1170+
11071171
## Import Statement
11081172

11091173
Often you want to bring some values from a table into the current scope as

0 commit comments

Comments
 (0)