When a function returns a table, MoonScript allows us to use a destructuring assignment to assign values from the table directly to variables in our scope.
However, when we mix a destructuring assignment with assigning from additional return values from the function, the compiled code behaves as if those additional return values are all nil.
Here's a minimal reproduction scenario to clarify:
fun = () ->
my_table = { a: 1, b: 2 }
return my_table, 2
{ a: my_a, b: my_b }, my_c = fun!
This generates the following Lua code:
local fun
fun = function()
local my_table = {
a = 1,
b = 2
}
return my_table, 2
end
local my_a, my_b
do
local _obj_0 = fun()
my_a, my_b = _obj_0.a, _obj_0.b
end
local my_c = nil
When the proper output should be along the lines of:
local fun
fun = function()
local my_table = {
a = 1,
b = 2
}
return my_table, 2
end
local my_a, my_b, my_c
do
local _obj_0, my_c = fun()
my_a, my_b = _obj_0.a, _obj_0.b
end
I'm seeing this issue with the latest MoonScript release, 0.5.0-1.
This doesn't sound like a terribly difficult fix, so if no one starts working on it soon, I'll probably be able to fix it and contribute a pull request.
When a function returns a table, MoonScript allows us to use a destructuring assignment to assign values from the table directly to variables in our scope.
However, when we mix a destructuring assignment with assigning from additional return values from the function, the compiled code behaves as if those additional return values are all nil.
Here's a minimal reproduction scenario to clarify:
This generates the following Lua code:
When the proper output should be along the lines of:
I'm seeing this issue with the latest MoonScript release,
0.5.0-1.This doesn't sound like a terribly difficult fix, so if no one starts working on it soon, I'll probably be able to fix it and contribute a pull request.