Skip to content

Cannot use destructuring assignment with multiple return values #304

Description

@geomaster

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:

localfunfun=function()
localmy_table= {
a=1,
b=2
}
returnmy_table, 2endlocalmy_a, my_bdolocal_obj_0=fun()
my_a, my_b=_obj_0.a, _obj_0.bendlocalmy_c=nil

When the proper output should be along the lines of:

localfunfun=function()
localmy_table= {
a=1,
b=2
}
returnmy_table, 2endlocalmy_a, my_b, my_cdolocal_obj_0, my_c=fun()
my_a, my_b=_obj_0.a, _obj_0.bend

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions