Do
When used as a statement, do works just like it does in Lua.
MoonScript:
do
var = "hello"
print var
print var -- nil here
Lua:
do
local var = "hello"
print(var)
end
print(var)
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.
MoonScript:
counter = do
i = 0
->
i += 1
i
print counter!
print counter!
Lua:
local counter
do
local i = 0
counter = function()
i = i + 1
return i
end
end
print(counter())
print(counter())
MoonScript:
tbl = {
key: do
print "assigning key!"
1234
}
Lua:
local tbl = {
key = (function()
print("assigning key!")
return 1234
end)()
}