2015-12-28 05:02:57 +00:00
|
|
|
{
|
|
|
|
target: "reference/index"
|
|
|
|
template: "reference"
|
|
|
|
title: "Language Guide"
|
|
|
|
short_name: "lang"
|
|
|
|
}
|
2014-06-19 06:45:47 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
MoonScript is a programming language that compiles to
|
|
|
|
[Lua](http://www.lua.org). This guide expects the reader to have basic
|
|
|
|
familiarity with Lua. For each code snippet below, the MoonScript is on the
|
|
|
|
left and the compiled Lua is on right right.
|
|
|
|
|
2013-11-20 04:12:08 +00:00
|
|
|
This is the official language reference manual, installation directions and the
|
2012-02-07 16:52:25 +00:00
|
|
|
homepage are located at <http://moonscript.org>.
|
|
|
|
|
|
|
|
<div class="github-buttons">
|
2012-08-09 14:38:42 +00:00
|
|
|
<iframe src="http://ghbtns.com/github-btn.html?user=leafo&repo=moonscript&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="110px" height="20px"></iframe>
|
|
|
|
<iframe src="http://ghbtns.com/github-btn.html?user=leafo&repo=moonscript&type=fork&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="95px" height="20px"></iframe>
|
2012-02-07 16:52:25 +00:00
|
|
|
</div>
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
# The Language
|
|
|
|
|
2013-07-01 05:49:57 +00:00
|
|
|
## Whitespace
|
|
|
|
|
|
|
|
MoonScript is a whitespace sensitive language. This means that
|
|
|
|
instead of using `do` and `end` (or `{` and `}`) to delimit sections of code we
|
|
|
|
use line-breaks and indentation.
|
|
|
|
|
2014-06-19 06:45:47 +00:00
|
|
|
This means that how you indent your code is important. Luckily MoonScript
|
|
|
|
doesn't care how you do it but only requires that you be consistent.
|
2013-07-01 05:49:57 +00:00
|
|
|
|
|
|
|
An indent must be at least 1 space or 1 tab, but you can use as many as you
|
|
|
|
like. All the code snippets on this page will use two spaces.
|
|
|
|
|
|
|
|
> Should you happen to mix tabs and spaces, a tab is equivalent to 4 spaces. I
|
|
|
|
> shouldn't be telling you this though because you should never do it.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Assignment
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
Assigning to an undeclared name will cause it to be declared as a new local
|
|
|
|
variable. The language is dynamically typed so you can assign any value to any
|
|
|
|
variable. You can assign multiple names and values at once just like Lua:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
hello = "world"
|
|
|
|
a,b,c = 1, 2, 3
|
|
|
|
hello = 123 -- uses the existing variable
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
If you wish to create a global variable it must be done using the
|
2013-01-12 19:04:26 +00:00
|
|
|
[`export`](#export_statement) keyword.
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-01-12 19:04:26 +00:00
|
|
|
The [`local`](#local_statement) keyword can be used to forward declare a
|
|
|
|
variable, or shadow an existing one.
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Update Assignment
|
|
|
|
|
2016-09-26 08:53:43 +00:00
|
|
|
`+=`, `-=`, `/=`, `*=`, `%=`, `..=`, `or=`, `and=`, `&=`, `|=`, `>>=`, and
|
|
|
|
`<<=` operators have been added for updating and assigning at the same time.
|
|
|
|
They are aliases for their expanded equivalents.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
x = 0
|
|
|
|
x += 10
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
s = "hello "
|
|
|
|
s ..= "world"
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
b = false
|
|
|
|
b and= true or false
|
2016-09-26 08:53:43 +00:00
|
|
|
|
|
|
|
p = 50
|
|
|
|
p &= 5
|
|
|
|
p |= 3
|
|
|
|
p >>= 3
|
|
|
|
p <<= 3
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
## Comments
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Like Lua, comments start with `--` and continue to the end of the line.
|
|
|
|
Comments are not written to the output.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
-- I am a comment
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
## Literals & Operators
|
|
|
|
|
2013-04-18 04:19:40 +00:00
|
|
|
All of the primitive literals in Lua can be used. This applies to numbers,
|
|
|
|
strings, booleans, and `nil`.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:19:40 +00:00
|
|
|
All of Lua's binary and unary operators are available. Additionally `!=` is as
|
|
|
|
an alias for `~=`.
|
|
|
|
|
|
|
|
Unlike Lua, Line breaks are allowed inside of single and double quote strings
|
|
|
|
without an escape sequence:
|
|
|
|
|
|
|
|
```moon
|
|
|
|
some_string = "Here is a string
|
2014-03-06 21:05:17 +00:00
|
|
|
that has a line break in it."
|
2013-04-18 04:19:40 +00:00
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
## Function Literals
|
|
|
|
|
|
|
|
All functions are created using a function expression. A simple function is
|
|
|
|
denoted using the arrow: `->`
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_function = ->
|
|
|
|
my_function() -- call the empty function
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The body of the function can either be one statement placed directly after the
|
|
|
|
arrow, or it can be a series of statements indented on the following lines:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
func_a = -> print "hello world"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
func_b = ->
|
|
|
|
value = 100
|
|
|
|
print "The value:", value
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
If a function has no arguments, it can be called using the `!` operator,
|
|
|
|
instead of empty parentheses. The `!` invocation is the preferred way to call
|
|
|
|
functions with no arguments.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
func_a!
|
|
|
|
func_b()
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Functions with arguments can be created by preceding the arrow with a list of
|
|
|
|
argument names in parentheses:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
sum = (x, y) -> print "sum", x + y
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-11-20 04:12:08 +00:00
|
|
|
Functions can be called by listing the arguments after the name of an expression
|
2012-11-04 01:19:31 +00:00
|
|
|
that evaluates to a function. When chaining together function calls, the
|
|
|
|
arguments are applied to the closest function to the left.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
sum 10, 20
|
|
|
|
print sum 10, 20
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
a b c "a", "b", "c"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
In order to avoid ambiguity in when calling functions, parentheses can also be
|
|
|
|
used to surround the arguments. This is required here in order to make sure the
|
|
|
|
right arguments get sent to the right functions.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
print "x:", sum(10, 20), "y:", sum(30, 40)
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
There must not be any space between the opening parenthesis and the function.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
Functions will coerce the last statement in their body into a return statement,
|
|
|
|
this is called implicit return:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
sum = (x, y) -> x + y
|
|
|
|
print "The sum is ", sum 10, 20
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
And if you need to explicitly return, you can use the `return` keyword:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
sum = (x, y) -> return x + y
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Just like in Lua, functions can return multiple values. The last statement must
|
|
|
|
be a list of values separated by commas:
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
mystery = (x, y) -> x + y, x - y
|
|
|
|
a,b = mystery 10, 20
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
### Fat Arrows
|
|
|
|
|
|
|
|
Because it is an idiom in Lua to send an object as the first argument when
|
|
|
|
calling a method, a special syntax is provided for creating functions which
|
|
|
|
automatically includes a `self` argument.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
func = (num) => @value + num
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
### Argument Defaults
|
|
|
|
|
|
|
|
It is possible to provide default values for the arguments of a function. An
|
2013-01-12 19:04:26 +00:00
|
|
|
argument is determined to be empty if its value is `nil`. Any `nil` arguments
|
2011-08-29 15:51:04 +00:00
|
|
|
that have a default value will be replace before the body of the function is run.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_function = (name="something", height=100) ->
|
|
|
|
print "Hello I am", name
|
|
|
|
print "My height is", height
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
An argument default value expression is evaluated in the body of the function
|
|
|
|
in the order of the argument declarations. For this reason default values have
|
|
|
|
access to previously declared arguments.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
some_args = (x=100, y=x+1000) ->
|
|
|
|
print x + y
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
### Considerations
|
|
|
|
|
|
|
|
Because of the expressive parentheses-less way of calling functions, some
|
|
|
|
restrictions must be put in place to avoid parsing ambiguity involving
|
|
|
|
whitespace.
|
|
|
|
|
|
|
|
The minus sign plays two roles, a unary negation operator and a binary
|
2013-04-16 18:16:47 +00:00
|
|
|
subtraction operator. Consider how the following examples compile:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
a = x - 10
|
|
|
|
b = x-10
|
|
|
|
c = x -y
|
|
|
|
d = x- z
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-16 18:16:47 +00:00
|
|
|
The precedence of the first argument of a function call can be controlled using
|
|
|
|
whitespace if the argument is a literal string. In Lua, it is common to leave
|
|
|
|
off parentheses when calling a function with a single string or table literal.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
When there is no space between a variable and a string literal, the
|
|
|
|
function call takes precedence over any following expressions. No other
|
|
|
|
arguments can be passed to the function when it is called this way.
|
|
|
|
|
|
|
|
Where there is a space following a variable and a string literal, the function
|
|
|
|
call acts as show above. The string literal belongs to any following
|
|
|
|
expressions (if they exist), which serves as the argument list.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
x = func"hello" + 100
|
|
|
|
y = func "hello" + 100
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
### Multi-line arguments
|
|
|
|
|
|
|
|
When calling functions that take a large number of arguments, it is convenient
|
|
|
|
to split the argument list over multiple lines. Because of the white-space
|
|
|
|
sensitive nature of the language, care must be taken when splitting up the
|
|
|
|
argument list.
|
|
|
|
|
|
|
|
If an argument list is to be continued onto the next line, the current line
|
|
|
|
must end in a comma. And the following line must be indented more than the
|
|
|
|
current indentation. Once indented, all other argument lines must be at the
|
|
|
|
same level of indentation to be part of the argument list
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_func 5,4,3,
|
|
|
|
8,9,10
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
cool_func 1,2,
|
|
|
|
3,4,
|
|
|
|
5,6,
|
|
|
|
7,8
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
This type of invocation can be nested. The level of indentation is used to
|
|
|
|
determine to which function the arguments belong to.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_func 5,6,7,
|
|
|
|
6, another_func 6,7,8,
|
|
|
|
9,1,2,
|
|
|
|
5,4
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Because [tables](#table_literals) also use the comma as a delimiter, this
|
|
|
|
indentation syntax is helpful for letting values be part of the argument list
|
|
|
|
instead of being part of the table.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
x = {
|
|
|
|
1,2,3,4, a_func 4,5,
|
|
|
|
5,6,
|
|
|
|
8,9,10
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Although uncommon, notice how we can give a deeper indentation for function
|
2012-11-04 01:19:31 +00:00
|
|
|
arguments if we know we will be using a lower indentation further on.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
y = { my_func 1,2,3,
|
|
|
|
4,5,
|
|
|
|
5,6,7
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The same thing can be done with other block level statements like
|
2011-09-17 20:08:33 +00:00
|
|
|
[conditionals](#conditionals). We can use indentation level to determine what
|
2011-08-29 15:51:04 +00:00
|
|
|
statement a value belongs to:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
if func 1,2,3,
|
|
|
|
"hello",
|
|
|
|
"world"
|
|
|
|
print "hello"
|
|
|
|
print "I am inside if"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
if func 1,2,3,
|
|
|
|
"hello",
|
|
|
|
"world"
|
|
|
|
print "hello"
|
|
|
|
print "I am inside if"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
## Table Literals
|
|
|
|
|
|
|
|
Like in Lua, tables are delimited in curly braces.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
some_values = { 1, 2, 3, 4 }
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Unlike Lua, assigning a value to a key in a table is done with `:` (instead of
|
|
|
|
`=`).
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
some_values = {
|
|
|
|
name: "Bill",
|
|
|
|
age: 200,
|
|
|
|
["favorite food"]: "rice"
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The curly braces can be left off if a single table of key value pairs is being
|
|
|
|
assigned.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
profile =
|
|
|
|
height: "4 feet",
|
|
|
|
shoe_size: 13,
|
|
|
|
favorite_foods: {"ice cream", "donuts"}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Newlines can be used to delimit values instead of a comma (or both):
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
values = {
|
|
|
|
1,2,3,4
|
|
|
|
5,6,7,8
|
|
|
|
name: "superman"
|
|
|
|
occupation: "crime fighting"
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
When creating a single line table literal, the curly braces can also be left
|
|
|
|
off:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_function dance: "Tango", partner: "none"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
y = type: "dog", legs: 4, tails: 1
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The keys of a table literal can be language keywords without being escaped:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
tbl = {
|
|
|
|
do: "something"
|
|
|
|
end: "hunger"
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
If you are constructing a table out of variables and wish the keys to be the
|
|
|
|
same as the variable names, then the `:` prefix operator can be used:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
hair = "golden"
|
|
|
|
height = 200
|
|
|
|
person = { :hair, :height, shoe_size: 40 }
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print_table :hair, :height
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-09-09 01:40:15 +00:00
|
|
|
If you want the key of a field in the table to to be result of an expression,
|
|
|
|
then you can wrap it in `[` `]`, just like in Lua. You can also use a string
|
|
|
|
literal directly as a key, leaving out the square brackets. This is useful if
|
2012-11-04 01:19:31 +00:00
|
|
|
your key has any special characters.
|
2012-09-09 01:40:15 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
t = {
|
|
|
|
[1 + 2]: "hello"
|
|
|
|
"hello world": true
|
|
|
|
}
|
2012-09-09 01:40:15 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2012-09-09 01:40:15 +00:00
|
|
|
|
2011-11-15 03:49:12 +00:00
|
|
|
## Comprehensions
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
Comprehensions provide a convenient syntax for constructing a new table by
|
|
|
|
iterating over some existing object and applying an expression to its values.
|
|
|
|
There are two kinds of comprehensions: list comprehensions and table
|
|
|
|
comprehensions. They both produce Lua tables; _list comprehensions_ accumulate
|
|
|
|
values into an array-like table, and _table comprehensions_ let you set both
|
|
|
|
the key and the value on each iteration.
|
2011-11-15 03:49:12 +00:00
|
|
|
|
|
|
|
### List Comprehensions
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The following creates a copy of the `items` table but with all the values
|
|
|
|
doubled.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
items = { 1, 2, 3, 4 }
|
|
|
|
doubled = [item * 2 for i, item in ipairs items]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The items included in the new table can be restricted with a `when` clause:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
iter = ipairs items
|
|
|
|
slice = [item for i, item in iter when i > 1 and i < 3]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Because it is common to iterate over the values of a numerically indexed table,
|
|
|
|
an `*` operator is introduced. The doubled example can be rewritten as:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
doubled = [item * 2 for item in *items]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The `for` and `when` clauses can be chained as much as desired. The only
|
|
|
|
requirement is that a comprehension has at least one `for` clause.
|
|
|
|
|
|
|
|
Using multiple `for` clauses is the same as using nested loops:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
x_coords = {4, 5, 6, 7}
|
|
|
|
y_coords = {9, 2, 3}
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
points = [{x,y} for x in *x_coords for y in *y_coords]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-01-12 23:09:17 +00:00
|
|
|
Numeric for loops can also be used in comprehensions:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
evens = [i for i=1,100 when i % 2 == 0]
|
|
|
|
```
|
2013-01-12 23:09:17 +00:00
|
|
|
|
2011-11-15 03:49:12 +00:00
|
|
|
### Table Comprehensions
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
The syntax for table comprehensions is very similar, only differing by using `{` and
|
2011-11-15 03:49:12 +00:00
|
|
|
`}` and taking two values from each iteration.
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
This example makes a copy of the table`thing`:
|
2011-11-15 03:49:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
thing = {
|
|
|
|
color: "red"
|
|
|
|
name: "fast"
|
|
|
|
width: 123
|
|
|
|
}
|
2011-11-15 03:49:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
thing_copy = {k,v for k,v in pairs thing}
|
|
|
|
```
|
2011-11-15 03:49:12 +00:00
|
|
|
|
|
|
|
Table comprehensions, like list comprehensions, also support multiple `for` and
|
2011-12-27 19:35:16 +00:00
|
|
|
`when` clauses. In this example we use a `when` clause to prevent the value
|
2011-11-15 03:49:12 +00:00
|
|
|
associated with the `color` key from being copied.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
no_color = {k,v for k,v in pairs thing when k != "color"}
|
|
|
|
```
|
2011-11-15 03:49:12 +00:00
|
|
|
|
|
|
|
The `*` operator is also supported. Here we create a square root look up table
|
|
|
|
for a few numbers.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
numbers = {1,2,3,4}
|
|
|
|
sqrts = {i, math.sqrt i for i in *numbers}
|
|
|
|
```
|
2011-11-15 03:49:12 +00:00
|
|
|
|
2012-10-03 18:30:23 +00:00
|
|
|
The key-value tuple in a table comprehension can also come from a single
|
|
|
|
expression, in which case the expression should return two values. The
|
|
|
|
first is used as the key and the second is used as the value:
|
|
|
|
|
|
|
|
In this example we convert an array of pairs to a table where the first item in
|
|
|
|
the pair is the key and the second is the value.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
tuples = {{"hello", "world"}, {"foo", "bar"}}
|
|
|
|
tbl = {unpack tuple for tuple in *tuples}
|
|
|
|
```
|
2012-10-03 18:30:23 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
### Slicing
|
|
|
|
|
|
|
|
A special syntax is provided to restrict the items that are iterated over when
|
|
|
|
using the `*` operator. This is equivalent to setting the iteration bounds and
|
|
|
|
a step size in a `for` loop.
|
|
|
|
|
|
|
|
Here we can set the minimum and maximum bounds, taking all items with indexes
|
|
|
|
between 1 and 5 inclusive:
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
slice = [item for item in *items[1,5]]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Any of the slice arguments can be left off to use a sensible default. In this
|
|
|
|
example, if the max index is left off it defaults to the length of the table.
|
|
|
|
This will take everything but the first element:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
slice = [item for item in *items[2,]]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
If the minimum bound is left out, it defaults to 1. Here we only provide a step
|
|
|
|
size and leave the other bounds blank. This takes all odd indexed items: (1, 3,
|
|
|
|
5, ...)
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
slice = [item for item in *items[,,2]]
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
## String Interpolation
|
|
|
|
|
|
|
|
You can mix expressions into string literals using `#{}` syntax.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
print "I am #{math.random! * 100}% sure."
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
String interpolation is only available in double quoted strings.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## For Loop
|
|
|
|
|
|
|
|
There are two for loop forms, just like in Lua. A numeric one and a generic one:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
for i = 10, 20
|
|
|
|
print i
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
for k = 1,15,2 -- an optional step provided
|
|
|
|
print k
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
for key, value in pairs object
|
|
|
|
print key, value
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2012-10-03 18:30:23 +00:00
|
|
|
The slicing and `*` operators can be used, just like with comprehensions:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
for item in *items[2,4]
|
|
|
|
print item
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
A shorter syntax is also available for all variations when the body is only a
|
|
|
|
single line:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
for item in *items do print item
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
for j = 1,10,3 do print j
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2014-01-04 07:27:35 +00:00
|
|
|
A for loop can also be used as an expression. The last statement in the body of
|
2011-08-29 15:51:04 +00:00
|
|
|
the for loop is coerced into an expression and appended to an accumulating
|
2013-01-25 01:15:20 +00:00
|
|
|
array table.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Doubling every even number:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
doubled_evens = for i=1,20
|
|
|
|
if i % 2 == 0
|
|
|
|
i * 2
|
|
|
|
else
|
|
|
|
i
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-01-25 01:15:20 +00:00
|
|
|
You can also filter values by combining the for loop expression with the
|
|
|
|
[`continue`](#continue) statement.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
For loops at the end of a function body are not accumulated into a table for a
|
2012-11-04 01:19:31 +00:00
|
|
|
return value (Instead the function will return `nil`). Either an explicit
|
2011-08-29 15:51:04 +00:00
|
|
|
`return` statement can be used, or the loop can be converted into a list
|
|
|
|
comprehension.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
func_a = -> for i=1,10 do i
|
|
|
|
func_b = -> return for i=1,10 do i
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print func_a! -- prints nil
|
|
|
|
print func_b! -- prints table object
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
This is done to avoid the needless creation of tables for functions that don't
|
|
|
|
need to return the results of the loop.
|
|
|
|
|
|
|
|
## While Loop
|
|
|
|
|
|
|
|
The while loop also comes in two variations:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
i = 10
|
|
|
|
while i > 0
|
|
|
|
print i
|
|
|
|
i -= 1
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
while running == true do my_function!
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Like for loops, the while loop can also be used an expression. Additionally,
|
|
|
|
for a function to return the accumulated value of a while loop, the statement
|
|
|
|
must be explicitly returned.
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
## Continue
|
|
|
|
|
|
|
|
A `continue` statement can be used to skip the current iteration in a loop.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
i = 0
|
|
|
|
while i < 10
|
2013-07-01 05:49:57 +00:00
|
|
|
continue if i % 2 == 0
|
2013-04-18 04:05:01 +00:00
|
|
|
print i
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
`continue` can also be used with loop expressions to prevent that iteration
|
|
|
|
from accumulating into the result. This examples filters the array table into
|
|
|
|
just even numbers:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_numbers = {1,2,3,4,5,6}
|
|
|
|
odds = for x in *my_numbers
|
|
|
|
continue if x % 2 == 1
|
|
|
|
x
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Conditionals
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
have_coins = false
|
|
|
|
if have_coins
|
|
|
|
print "Got coins"
|
|
|
|
else
|
|
|
|
print "No coins"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
A short syntax for single statements can also be used:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
have_coins = false
|
|
|
|
if have_coins then print "Got coins" else print "No coins"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2014-01-03 06:27:06 +00:00
|
|
|
Because if statements can be used as expressions, this can also be written as:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
have_coins = false
|
|
|
|
print if have_coins then "Got coins" else "No coins"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Conditionals can also be used in return statements and assignments:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
is_tall = (name) ->
|
|
|
|
if name == "Rob"
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
message = if is_tall "Rob"
|
|
|
|
"I am very tall"
|
|
|
|
else
|
|
|
|
"I am not so tall"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print message -- prints: I am very tall
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-07-02 05:56:17 +00:00
|
|
|
The opposite of `if` is `unless`:
|
|
|
|
|
|
|
|
```moon
|
|
|
|
unless os.date("%A") == "Monday"
|
|
|
|
print "it is not Monday!"
|
|
|
|
```
|
|
|
|
|
|
|
|
```moon
|
|
|
|
print "You're lucky!" unless math.random! > 0.1
|
|
|
|
```
|
|
|
|
|
2012-11-04 21:39:42 +00:00
|
|
|
### With Assignment
|
|
|
|
|
2012-10-01 19:37:47 +00:00
|
|
|
`if` and `elseif` blocks can take an assignment in place of a conditional
|
|
|
|
expression. Upon evaluating the conditional, the assignment will take place and
|
|
|
|
the value that was assigned to will be used as the conditional expression. The
|
|
|
|
assigned variable is only in scope for the body of the conditional, meaning it
|
|
|
|
is never available if the value is not truthy.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
if user = database.find_user "moon"
|
|
|
|
print user.name
|
|
|
|
```
|
2012-10-01 19:37:47 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
if hello = os.getenv "hello"
|
|
|
|
print "You have hello", hello
|
|
|
|
elseif world = os.getenv "world"
|
|
|
|
print "you have world", world
|
|
|
|
else
|
|
|
|
print "nothing :("
|
|
|
|
```
|
2012-11-04 21:39:42 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Line Decorators
|
|
|
|
|
|
|
|
For convenience, the for loop and if statement can be applied to single
|
|
|
|
statements at the end of the line:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
print "hello world" if name == "Rob"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
And with basic loops:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
print "item: ", item for item in *items
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2011-11-19 03:18:22 +00:00
|
|
|
## Switch
|
|
|
|
|
|
|
|
The switch statement is shorthand for writing a series of if statements that
|
|
|
|
check against the same value. Note that the value is only evaluated once. Like
|
|
|
|
if statements, switches can have an else block to handle no matches. Comparison
|
|
|
|
is done with the `==` operator.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
name = "Dan"
|
|
|
|
switch name
|
|
|
|
when "Robert"
|
2013-11-20 04:12:08 +00:00
|
|
|
print "You are Robert"
|
2013-04-18 04:05:01 +00:00
|
|
|
when "Dan", "Daniel"
|
|
|
|
print "Your name, it's Dan"
|
|
|
|
else
|
|
|
|
print "I don't know about your name"
|
|
|
|
```
|
2011-11-19 03:18:22 +00:00
|
|
|
|
2013-01-25 02:44:14 +00:00
|
|
|
A switch `when` clause can match against multiple values by listing them out
|
|
|
|
comma separated.
|
|
|
|
|
2011-11-19 03:18:22 +00:00
|
|
|
Switches can be used as expressions as well, here we can assign the result of
|
|
|
|
the switch to a variable:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
b = 1
|
|
|
|
next_number = switch b
|
|
|
|
when 1
|
|
|
|
2
|
|
|
|
when 2
|
|
|
|
3
|
|
|
|
else
|
|
|
|
error "can't count that high!"
|
|
|
|
```
|
2011-11-19 03:18:22 +00:00
|
|
|
|
2011-12-11 06:09:36 +00:00
|
|
|
We can use the `then` keyword to write a switch's `when` block on a single line.
|
2011-11-19 03:18:22 +00:00
|
|
|
No extra keyword is needed to write the else block on a single line.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
msg = switch math.random(1, 5)
|
|
|
|
when 1 then "you are lucky"
|
|
|
|
when 2 then "you are almost lucky"
|
|
|
|
else "not so lucky"
|
|
|
|
```
|
2011-11-19 03:18:22 +00:00
|
|
|
|
|
|
|
It is worth noting the order of the case comparison expression. The case's
|
|
|
|
expression is on the left hand side. This can be useful if the case's
|
|
|
|
expression wants to overwrite how the comparison is done by defining an `eq`
|
|
|
|
metamethod.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Object Oriented Programming
|
|
|
|
|
|
|
|
In these examples, the generated Lua code may appear overwhelming. It is best
|
|
|
|
to focus on the meaning of the MoonScript code at first, then look into the Lua
|
|
|
|
code if you wish to know the implementation details.
|
|
|
|
|
|
|
|
A simple class:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
class Inventory
|
|
|
|
new: =>
|
|
|
|
@items = {}
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
add_item: (name) =>
|
|
|
|
if @items[name]
|
|
|
|
@items[name] += 1
|
|
|
|
else
|
|
|
|
@items[name] = 1
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
A class is declared with a `class` statement followed by a table-like
|
|
|
|
declaration where all of the methods and properties are listed.
|
|
|
|
|
|
|
|
The `new` property is special in that it will become the constructor.
|
|
|
|
|
|
|
|
Notice how all the methods in the class use the fat arrow function syntax. When
|
|
|
|
calling methods on a instance, the instance itself is sent in as the first
|
|
|
|
argument. The fat arrow handles the creation of a `self` argument.
|
|
|
|
|
|
|
|
The `@` prefix on a variable name is shorthand for `self.`. `@items` becomes
|
|
|
|
`self.items`.
|
|
|
|
|
|
|
|
Creating an instance of the class is done by calling the name of the class as a
|
|
|
|
function.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
inv = Inventory!
|
|
|
|
inv\add_item "t-shirt"
|
|
|
|
inv\add_item "pants"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Because the instance of the class needs to be sent to the methods when they are
|
2012-01-09 07:16:12 +00:00
|
|
|
called, the <code>\\</code> operator is used.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
All properties of a class are shared among the instances. This is fine for
|
|
|
|
functions, but for other types of objects, undesired results may occur.
|
|
|
|
|
|
|
|
Consider the example below, the `clothes` property is shared amongst all
|
|
|
|
instances, so modifications to it in one instance will show up in another:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class Person
|
|
|
|
clothes: {}
|
|
|
|
give_item: (name) =>
|
|
|
|
table.insert @clothes, name
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
a = Person!
|
|
|
|
b = Person!
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
a\give_item "pants"
|
|
|
|
b\give_item "shirt"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- will print both pants and shirt
|
|
|
|
print item for item in *a.clothes
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The proper way to avoid this problem is to create the mutable state of the
|
|
|
|
object in the constructor:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class Person
|
|
|
|
new: =>
|
|
|
|
@clothes = {}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
### Inheritance
|
|
|
|
|
|
|
|
The `extends` keyword can be used in a class declaration to inherit the
|
|
|
|
properties and methods from another class.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
class BackPack extends Inventory
|
|
|
|
size: 10
|
|
|
|
add_item: (name) =>
|
|
|
|
if #@items > size then error "backpack is full"
|
|
|
|
super name
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Here we extend our Inventory class, and limit the amount of items it can carry.
|
2011-12-03 19:02:06 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
In this example, we don't define a constructor on the subclass, so the parent
|
|
|
|
class' constructor is called when we make a new instance. If we did define a
|
|
|
|
constructor then we can use the [`super`](#super) method to call the parent
|
|
|
|
constructor.
|
|
|
|
|
2012-09-09 00:52:26 +00:00
|
|
|
Whenever a class inherits from another, it sends a message to the parent class
|
2012-11-04 01:19:31 +00:00
|
|
|
by calling the method `__inherited` on the parent class if it exists. The
|
|
|
|
function receives two arguments, the class that is being inherited and the
|
2012-09-09 00:52:26 +00:00
|
|
|
child class.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class Shelf
|
|
|
|
@__inherited: (child) =>
|
|
|
|
print @__name, "was inherited by", child.__name
|
2012-09-09 00:52:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- will print: Shelf was inherited by Cupboard
|
|
|
|
class Cupboard extends Shelf
|
|
|
|
```
|
2012-09-09 00:52:26 +00:00
|
|
|
|
2011-12-03 19:02:06 +00:00
|
|
|
### Super
|
|
|
|
|
|
|
|
`super` is a special keyword that can be used in two different ways: It can be
|
|
|
|
treated as an object, or it can be called like a function. It only has special
|
|
|
|
functionality when inside a class.
|
|
|
|
|
|
|
|
When called as a function, it will call the function of the same name in the
|
|
|
|
parent class. The current `self` will automatically be passed as the first
|
|
|
|
argument. (As seen in the [inheritance](#inheritance) example above)
|
|
|
|
|
|
|
|
When `super` is used as a normal value, it is a reference to the parent class
|
|
|
|
object.
|
|
|
|
|
|
|
|
It can be accessed like any of object in order to retrieve values in the
|
|
|
|
parent class that might have been shadowed by the child class.
|
|
|
|
|
2011-12-11 06:19:12 +00:00
|
|
|
When the <code>\\</code> calling operator is used with `super`, `self` is inserted as the
|
2011-12-03 19:02:06 +00:00
|
|
|
first argument instead of the value of `super` itself. When using `.` to
|
|
|
|
retrieve a function, the raw function is returned.
|
|
|
|
|
|
|
|
A few examples of using `super` in different ways:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
class MyClass extends ParentClass
|
|
|
|
a_method: =>
|
|
|
|
-- the following have the same effect:
|
|
|
|
super "hello", "world"
|
|
|
|
super\a_method "hello", "world"
|
|
|
|
super.a_method self, "hello", "world"
|
2011-12-03 19:02:06 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- super as a value is equal to the parent class:
|
|
|
|
assert super == ParentClass
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2011-12-03 19:07:24 +00:00
|
|
|
`super` can also be used on left side of a [Function Stub](#function_stubs).
|
|
|
|
The only major difference is that instead of the resulting function being bound
|
|
|
|
to the value of `super`, it is bound to `self`.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
### Types
|
2012-01-09 07:16:12 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
Every instance of a class carries its type with it. This is stored in the
|
|
|
|
special `__class` property. This property holds the class object. The class
|
|
|
|
object is what we call to build a new instance. We can also index the class
|
|
|
|
object to retrieve class methods and properties.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
b = BackPack!
|
|
|
|
assert b.__class == BackPack
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print BackPack.size -- prints 10
|
|
|
|
```
|
2012-01-09 07:16:12 +00:00
|
|
|
### Class Objects
|
|
|
|
|
|
|
|
The class object is what we create when we use a `class` statement. The class
|
|
|
|
object is stored in a variable of the same name of the class.
|
|
|
|
|
|
|
|
The class object can be called like a function in order to create new
|
|
|
|
instances. That's how we created instances of classes in the examples above.
|
|
|
|
|
|
|
|
A class is made up of two tables. The class table itself, and the *base* table. The
|
|
|
|
*base* is used as the metatable for all the instances. All properties listed in
|
|
|
|
the class declaration are placed in the *base*.
|
|
|
|
|
|
|
|
The class object's metatable reads properties from the base if they don't exist
|
|
|
|
in the class object. This means we can access functions and properties directly
|
|
|
|
from the class.
|
|
|
|
|
|
|
|
It is important to note that assigning to the class object does not assign into
|
|
|
|
the *base*, so it's not a valid way to add new methods to instances. Instead
|
|
|
|
the *base* must explicitly be changed. See the `__base` field below.
|
|
|
|
|
|
|
|
The class object has a couple special properties:
|
|
|
|
|
|
|
|
The name of the class as when it was declared is stored as a string in the
|
|
|
|
`__name` field of the class object.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
print BackPack.__name -- prints Backpack
|
|
|
|
```
|
2012-01-09 07:16:12 +00:00
|
|
|
|
|
|
|
The *base* object is stored in `__base`. We can modify this table to add
|
|
|
|
functionality to instances that have already been created and ones that are yet
|
|
|
|
to be created.
|
|
|
|
|
|
|
|
If the class extends from anything, the parent class object is stored in
|
|
|
|
`__parent`.
|
|
|
|
|
|
|
|
### Class Variables
|
|
|
|
|
|
|
|
We can create variables directly in the class object instead of in the *base*
|
|
|
|
by using `@` in the front of the property name in a class declaration.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class Things
|
|
|
|
@some_func: => print "Hello from", @__name
|
2012-01-09 07:16:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
Things\some_func!
|
2012-01-09 07:16:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- class variables not visible in instances
|
|
|
|
assert Things().some_func == nil
|
2012-01-09 07:16:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-01-09 07:16:12 +00:00
|
|
|
In expressions, we can use `@@` to access a value that is stored in the
|
|
|
|
`__class` of `self`. Thus, `@@hello` is shorthand for `self.__class.hello`.
|
2012-01-09 06:02:43 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class Counter
|
|
|
|
@count: 0
|
2012-01-09 07:16:12 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
new: =>
|
|
|
|
@@count += 1
|
2012-01-09 06:02:43 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
Counter!
|
|
|
|
Counter!
|
2012-01-09 06:02:43 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print Counter.count -- prints 2
|
|
|
|
```
|
2012-01-09 06:02:43 +00:00
|
|
|
|
|
|
|
The calling semantics of `@@` are similar to `@`. Calling a `@@` name will pass
|
|
|
|
the class in as the first argument using Lua's colon syntax.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
@@hello 1,2,3,4
|
|
|
|
```
|
2012-01-09 07:16:12 +00:00
|
|
|
### Class Declaration Statements
|
|
|
|
|
|
|
|
In the body of a class declaration, we can have normal expressions in addition
|
|
|
|
to key/value pairs. In this context, `self` is equal to the class object.
|
|
|
|
|
|
|
|
Here is an alternative way to create a class variable compared to what's
|
|
|
|
described above:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
class Things
|
|
|
|
@class_var = "hello world"
|
2012-01-22 19:52:46 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2012-01-09 07:16:12 +00:00
|
|
|
|
|
|
|
These expressions are executed after all the properties have been added to the
|
|
|
|
*base*.
|
2012-01-09 06:02:43 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
All variables declared in the body of the class are local to the classes
|
|
|
|
properties. This is convenient for placing private values or helper functions
|
|
|
|
that only the class methods can access:
|
|
|
|
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
class MoreThings
|
|
|
|
secret = 123
|
|
|
|
log = (msg) -> print "LOG:", msg
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
some_method: =>
|
|
|
|
log "hello world: " .. secret
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2012-01-11 06:14:14 +00:00
|
|
|
|
|
|
|
### `@` and `@@` Values
|
|
|
|
|
|
|
|
When `@` and `@@` are prefixed in front of a name they represent, respectively,
|
|
|
|
that name accessed in `self` and `self.__class`.
|
|
|
|
|
|
|
|
If they are used all by themselves, they are aliases for `self` and
|
|
|
|
`self.__class`.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
assert @ == self
|
|
|
|
assert @@ == self.__class
|
|
|
|
```
|
2012-01-11 06:14:14 +00:00
|
|
|
|
|
|
|
For example, a quick way to create a new instance of the same class from an
|
|
|
|
instance method using `@@`:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
some_instance_method = (...) => @@ ...
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
### Class Expressions
|
|
|
|
|
|
|
|
The `class` syntax can also be used as an expression which can be assigned to a
|
|
|
|
variable or explicitly returned.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
x = class Bucket
|
|
|
|
drops: 0
|
|
|
|
add_drop: => @drops += 1
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
|
|
|
### Anonymous classes
|
|
|
|
|
|
|
|
The name can be left out when declaring a class. The `__name` attribute will be
|
|
|
|
`nil`, unless the class expression is in an assignment. The name on the left
|
|
|
|
hand side of the assignment is used instead of `nil`.
|
|
|
|
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
BigBucket = class extends Bucket
|
|
|
|
add_drop: => @drops += 10
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
assert Bucket.__name == "BigBucket"
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
|
|
|
You can even leave off the body, meaning you can write a blank anonymous class
|
|
|
|
like this:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moononly
|
|
|
|
x = class
|
|
|
|
```
|
2012-01-11 06:14:14 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Export Statement
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
Because all assignments to variables that are not lexically visible will
|
2011-08-29 15:51:04 +00:00
|
|
|
be declared as local, special syntax is required to declare a variable globally.
|
|
|
|
|
|
|
|
The export keyword makes it so any following assignments to the specified names
|
|
|
|
will not be assigned locally.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
export var_name, var_name2
|
|
|
|
var_name, var_name3 = "hello", "world"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
This is especially useful when declaring what will be externally visible in a
|
|
|
|
module:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
-- my_module.moon
|
|
|
|
module "my_module", package.seeall
|
|
|
|
export print_result
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
length = (x, y) -> math.sqrt x*x + y*y
|
2011-11-07 06:32:27 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print_result = (x, y) ->
|
|
|
|
print "Length is ", length x, y
|
2011-11-07 06:32:27 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- main.moon
|
|
|
|
require "my_module"
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_module.print_result 4, 5 -- prints the result
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print my_module.length 6, 7 -- errors, `length` not visible
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2011-11-07 06:32:27 +00:00
|
|
|
Assignment can be combined with the export keyword to assign to global
|
|
|
|
variables directly:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
export some_number, message_str = 100, "hello world"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2011-11-07 06:48:25 +00:00
|
|
|
Additionally, a class declaration can be prefixed with the export keyword in
|
|
|
|
order to export it.
|
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
Export will have no effect if there is already a local variable of the same
|
|
|
|
name in scope.
|
|
|
|
|
2011-10-02 07:08:13 +00:00
|
|
|
### Export All & Export Proper
|
|
|
|
|
|
|
|
The `export` statement can also take special symbols `*` and `^`.
|
|
|
|
|
2011-10-31 05:17:53 +00:00
|
|
|
`export *` will cause any name declared after the statement to be exported in the
|
2011-10-02 07:08:13 +00:00
|
|
|
current scope. `export ^` will export all proper names, names that begin with a
|
|
|
|
capital letter.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-01-12 19:04:26 +00:00
|
|
|
## Local Statement
|
|
|
|
|
|
|
|
Sometimes you want to declare a variable name before the first time you assign
|
|
|
|
it. The `local` statement can be used to do that.
|
|
|
|
|
|
|
|
In this example we declare the variable `a` in the outer scope so its value
|
|
|
|
can be accessed after the `if` statement. If there was no `local` statement then
|
|
|
|
`a` would only be accessible inside the `if` statement.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
local a
|
|
|
|
if something
|
|
|
|
a = 1
|
|
|
|
print a
|
|
|
|
```
|
2013-01-12 19:04:26 +00:00
|
|
|
|
|
|
|
`local` can also be used to shadow existing variables for the rest of a scope.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
x = 10
|
|
|
|
if something
|
|
|
|
local x
|
|
|
|
x = 12
|
|
|
|
print x -- prints 10
|
|
|
|
```
|
2013-01-12 19:04:26 +00:00
|
|
|
|
|
|
|
When you have one function that calls another, you typically order them such
|
|
|
|
that the second function can access the first. If both functions happen to
|
|
|
|
call each other, then you must forward declare the names:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
local first, second
|
2013-01-12 19:04:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
first = ->
|
|
|
|
second!
|
2013-01-12 19:04:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
second = ->
|
|
|
|
first!
|
|
|
|
```
|
2013-01-12 19:04:26 +00:00
|
|
|
|
|
|
|
The same problem occurs with declaring classes and regular values too.
|
|
|
|
|
|
|
|
Because forward declaring is often better than manually ordering your assigns,
|
|
|
|
a special form of `local` is provided:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
local *
|
2013-01-12 19:04:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
first = ->
|
|
|
|
print data
|
|
|
|
second!
|
2013-01-12 19:04:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
second = ->
|
|
|
|
first!
|
2013-01-12 19:04:26 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
data = {}
|
|
|
|
```
|
2013-01-12 19:04:26 +00:00
|
|
|
|
|
|
|
`local *` will forward declare all names below it in the current scope.
|
|
|
|
|
|
|
|
Similarly to [`export`](#export_all_and_export_proper) one more special form is
|
|
|
|
provided, `local ^`. This will forward declare all names that begin with a
|
|
|
|
capital letter.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## Import Statement
|
|
|
|
|
|
|
|
Often you want to bring some values from a table into the current scope as
|
|
|
|
local variables by their name. The import statement lets us accomplish this:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
import insert from table
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The multiple names can be given, each separated by a comma:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
import C, Ct, Cmt from lpeg
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Sometimes a function requires that the table be sent in as the first argument
|
2011-10-31 05:17:53 +00:00
|
|
|
(when using the <code>\\</code> syntax). As a shortcut, we can prefix the name
|
|
|
|
with a <code>\\</code> to bind it to that table:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
-- some object
|
|
|
|
my_module =
|
|
|
|
state: 100
|
|
|
|
add: (value) =>
|
|
|
|
self.state + value
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
import \add from my_module
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2016-03-29 04:18:31 +00:00
|
|
|
print add 22 -- equivalent to calling my_module\add 22
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2014-03-05 18:25:49 +00:00
|
|
|
When handing multiple imports you can substitute the comma with a newline and
|
|
|
|
any amount of whitespace. When working with a lot of imports you might write
|
|
|
|
something like this:
|
|
|
|
|
|
|
|
```moon
|
|
|
|
import
|
|
|
|
assert_csrf
|
|
|
|
assert_timezone
|
|
|
|
not_found
|
|
|
|
require_login
|
|
|
|
from require "helpers"
|
|
|
|
```
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## With Statement
|
|
|
|
|
|
|
|
A common pattern involving the creation of an object is calling a series of
|
|
|
|
functions and setting a series of properties immediately after creating it.
|
|
|
|
|
|
|
|
This results in repeating the name of the object multiple times in code, adding
|
|
|
|
unnecessary noise. A common solution to this is to pass a table in as an
|
|
|
|
argument which contains a collection of keys and values to overwrite. The
|
|
|
|
downside to this is that the constructor of this object must support this form.
|
|
|
|
|
2011-10-01 18:22:42 +00:00
|
|
|
The `with` block helps to alleviate this. Within a `with` block we can use a
|
2011-10-31 05:17:53 +00:00
|
|
|
special statements that begin with either `.` or <code>\\</code> which represent
|
2011-10-01 18:22:42 +00:00
|
|
|
those operations applied to the object we are using `with` on.
|
|
|
|
|
|
|
|
For example, we work with a newly created object:
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
with Person!
|
|
|
|
.name = "Oswald"
|
|
|
|
\add_relative my_dad
|
|
|
|
\save!
|
|
|
|
print .name
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
The `with` statement can also be used as an expression which returns the value
|
|
|
|
it has been giving access to.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
file = with File "favorite_foods.txt"
|
|
|
|
\set_encoding "utf8"
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
Or...
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
create_person = (name, relatives) ->
|
|
|
|
with Person!
|
|
|
|
.name = name
|
|
|
|
\add_relative relative for relative in *relatives
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
me = create_person "Leaf", {dad, mother, sister}
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
In this usage, `with` can be seen as a special form of the K combinator.
|
|
|
|
|
|
|
|
The expression in the `with` statement can also be an assignment, if you want
|
|
|
|
to give a name to the expression.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
with str = "Hello"
|
|
|
|
print "original:", str
|
|
|
|
print "upper:", \upper!
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
|
|
|
## Do
|
|
|
|
|
|
|
|
When used as a statement, `do` works just like it does in Lua.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
do
|
|
|
|
var = "hello"
|
|
|
|
print var
|
|
|
|
print var -- nil here
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
|
|
|
MoonScript's `do` can also be used an expression . Allowing you to combine
|
|
|
|
multiple lines into one. The result of the `do` expression is the last
|
|
|
|
statement in its body.
|
|
|
|
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
counter = do
|
|
|
|
i = 0
|
|
|
|
->
|
|
|
|
i += 1
|
|
|
|
i
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print counter!
|
|
|
|
print counter!
|
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
tbl = {
|
|
|
|
key: do
|
|
|
|
print "assigning key!"
|
|
|
|
1234
|
|
|
|
}
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-01-24 02:59:42 +00:00
|
|
|
## Destructuring Assignment
|
|
|
|
|
|
|
|
Destructuring assignment is a way to quickly extract values from a table by
|
|
|
|
their name or position in array based tables.
|
|
|
|
|
|
|
|
Typically when you see a table literal, `{1,2,3}`, it is on the right hand side
|
|
|
|
of an assignment because it is a value. Destructuring assignment swaps the role
|
|
|
|
of the table literal, and puts it on the left hand side of an assign
|
|
|
|
statement.
|
|
|
|
|
|
|
|
This is best explained with examples. Here is how you would unpack the first
|
|
|
|
two values from a table:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
thing = {1,2}
|
2013-01-24 02:59:42 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
{a,b} = thing
|
|
|
|
print a,b
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
|
|
|
|
In the destructuring table literal, the key represents the key to read from the
|
|
|
|
right hand side, and the value represents the name the read value will be
|
|
|
|
assigned to.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
obj = {
|
|
|
|
hello: "world"
|
|
|
|
day: "tuesday"
|
|
|
|
length: 20
|
|
|
|
}
|
2013-01-24 02:59:42 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
{hello: hello, day: the_day} = obj
|
|
|
|
print hello, the_day
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
|
|
|
|
This also works with nested data structures as well:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
obj2 = {
|
|
|
|
numbers: {1,2,3,4}
|
|
|
|
properties: {
|
|
|
|
color: "green"
|
|
|
|
height: 13.5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{numbers: {first, second}} = obj2
|
|
|
|
print first, second, color
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
If the destructuring statement is complicated, feel free to spread it out over
|
|
|
|
a few lines. A slightly more complicated example:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
{
|
|
|
|
numbers: { first, second }
|
|
|
|
properties: {
|
|
|
|
color: color
|
|
|
|
}
|
|
|
|
} = obj2
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
|
|
|
|
It's common to extract values from at table and assign them the local variables
|
|
|
|
that have the same name as the key. In order to avoid repetition we can use the
|
|
|
|
`:` prefix operator:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
{:concat, :insert} = table
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
|
|
|
|
This is effectively the same as import, but we can rename fields we want to
|
|
|
|
extract by mixing the syntax:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
{:mix, :max, random: rand } = math
|
|
|
|
```
|
2013-01-24 02:59:42 +00:00
|
|
|
|
2013-01-25 01:15:20 +00:00
|
|
|
### Destructuring In Other Places
|
|
|
|
|
|
|
|
Destructuring can also show up in places where an assignment implicitly takes
|
|
|
|
place. An example of this is a `for` loop:
|
|
|
|
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
tuples = {
|
|
|
|
{"hello", "world"}
|
|
|
|
{"egg", "head"}
|
|
|
|
}
|
2013-01-25 01:15:20 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
for {left, right} in *tuples
|
|
|
|
print left, right
|
|
|
|
```
|
2013-01-25 01:15:20 +00:00
|
|
|
|
|
|
|
We know each element in the array table is a two item tuple, so we can unpack
|
|
|
|
it directly in the names clause of the for statement using a destructure.
|
|
|
|
|
|
|
|
|
2011-10-01 18:46:47 +00:00
|
|
|
## Function Stubs
|
|
|
|
|
|
|
|
It is common to pass a function from an object around as a value, for example,
|
|
|
|
passing an instance method into a function as a callback. If the function
|
|
|
|
expects the object it is operating on as the first argument then you must
|
|
|
|
somehow bundle that object with the function so it can be called properly.
|
|
|
|
|
|
|
|
The function stub syntax is a shorthand for creating a new closure function
|
|
|
|
that bundles both the object and function. This new function calls the wrapped
|
|
|
|
function in the correct context of the object.
|
|
|
|
|
2011-10-01 20:05:57 +00:00
|
|
|
Its syntax is the same as calling an instance method with the <code>\\</code> operator but
|
2011-10-01 18:46:47 +00:00
|
|
|
with no argument list provided.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
my_object = {
|
|
|
|
value: 1000
|
|
|
|
write: => print "the value:", @value
|
|
|
|
}
|
2011-10-01 18:46:47 +00:00
|
|
|
|
2013-08-19 20:30:36 +00:00
|
|
|
run_callback = (func) ->
|
2013-04-18 04:05:01 +00:00
|
|
|
print "running callback..."
|
|
|
|
func!
|
2011-10-01 18:46:47 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- this will not work:
|
|
|
|
-- the function has to no reference to my_object
|
|
|
|
run_callback my_object.write
|
2011-10-01 18:46:47 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- function stub syntax
|
|
|
|
-- lets us bundle the object into a new function
|
|
|
|
run_callback my_object\write
|
|
|
|
```
|
2011-10-01 18:46:47 +00:00
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
## The Using Clause; Controlling Destructive Assignment
|
|
|
|
|
|
|
|
While lexical scoping can be a great help in reducing the complexity of the
|
|
|
|
code we write, things can get unwieldy as the code size increases. Consider
|
|
|
|
the following snippet:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
i = 100
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
-- many lines of code...
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func = ->
|
|
|
|
i = 10
|
|
|
|
while i > 0
|
|
|
|
print i
|
|
|
|
i -= 1
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func!
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
print i -- will print 0
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
In `my_func`, we've overwritten the value of `i` mistakenly. In this example it
|
|
|
|
is quite obvious, but consider a large, or foreign code base where it isn't
|
|
|
|
clear what names have already been declared.
|
|
|
|
|
|
|
|
It would be helpful to say which variables from the enclosing scope we intend
|
|
|
|
on change, in order to prevent us from changing others by accident.
|
|
|
|
|
|
|
|
The `using` keyword lets us do that. `using nil` makes sure that no closed
|
|
|
|
variables are overwritten in assignment. The `using` clause is placed after the
|
|
|
|
argument list in a function, or in place of it if there are no arguments.
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
i = 100
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func = (using nil) ->
|
|
|
|
i = "hello" -- a new local variable is created here
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func!
|
|
|
|
print i -- prints 100, i is unaffected
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
Multiple names can be separated by commas. Closure values can still be
|
|
|
|
accessed, they just cant be modified:
|
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moon
|
|
|
|
tmp = 1213
|
|
|
|
i, k = 100, 50
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func = (add using k,i) ->
|
|
|
|
tmp = tmp + add -- a new local tmp is created
|
|
|
|
i += tmp
|
|
|
|
k += tmp
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
my_func(22)
|
|
|
|
print i,k -- these have been updated
|
|
|
|
```
|
2011-08-29 15:51:04 +00:00
|
|
|
|
2012-11-04 01:19:31 +00:00
|
|
|
## Misc.
|
|
|
|
|
|
|
|
### Implicit Returns on Files
|
|
|
|
|
|
|
|
By default, a file will also implicitly return like a function. This is useful
|
|
|
|
for writing modules, where you can put your module's table as the last
|
|
|
|
statement in the file so it is returned when loaded with `require`.
|
|
|
|
|
|
|
|
### Writing Modules
|
|
|
|
|
|
|
|
Lua 5.2 has removed the `module` function for creating modules. It is
|
|
|
|
recommended to return a table instead when defining a module.
|
|
|
|
|
2014-03-05 18:25:49 +00:00
|
|
|
We can cleanly define modules by using the shorthand hash table key/value
|
|
|
|
syntax:
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2013-04-18 04:05:01 +00:00
|
|
|
```moonret
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2014-03-05 18:25:49 +00:00
|
|
|
MY_CONSTANT = "hello"
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2014-03-05 18:25:49 +00:00
|
|
|
my_function = -> print "the function"
|
|
|
|
my_second_function = -> print "another function"
|
|
|
|
|
|
|
|
{ :my_function, :my_second_function, :MY_CONSTANT}
|
2013-04-18 04:05:01 +00:00
|
|
|
```
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2014-03-05 18:25:49 +00:00
|
|
|
If you need to forward declare your values so you can access them regardless of
|
|
|
|
their written order you can add `local *` to the top of your file.
|
|
|
|
|
2011-08-29 15:51:04 +00:00
|
|
|
# License (MIT)
|
|
|
|
|
2017-01-06 23:43:33 +00:00
|
|
|
Copyright (C) 2017 by Leaf Corcoran
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2011-12-11 06:00:38 +00:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2011-12-11 06:00:38 +00:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2012-11-04 01:19:31 +00:00
|
|
|
|
2011-12-11 06:00:38 +00:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2011-08-29 15:51:04 +00:00
|
|
|
|
|
|
|
|