X Tutup
Skip to content

Allow numeric access to MultiReturn#962

Merged
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
hazzard993:feature/multi-access
Jan 7, 2021
Merged

Allow numeric access to MultiReturn#962
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
hazzard993:feature/multi-access

Conversation

@hazzard993
Copy link
Contributor

Resolves #960

function result() {
  return $multi(1, 2, 3);
}

const first = result()[0];

transforms to

function result() ... end

local first = select(
  1, -- uses +1 rule
  result()
)

But diagnostics will be generated if anything else about the type is attempted to be accessed

result()["forEach"] // not allowed to access via non-numeric type
result().forEach // not allowed to access via property access expression

@Perryvw Perryvw merged commit 3c9e369 into TypeScriptToLua:master Jan 7, 2021
@LoganDark
Copy link

That actually may be incorrect, it should be this:

local first = (select(
  1, -- uses +1 rule
  result()
))

Note the parenthesis around the call to select, this is necessary because select returns all arguments at the specific index, rather than just one.

Something like this:

local first, second = select(1, result()), 'second'

is incorrect because if result() returns more than one value, the second value will be assigned to second and the 'second' will be dropped.

This has correct behavior:

local first, second = (select(1, result())), 'second'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Indexing MultiReturn return value leads to incorrect Lua

3 participants

X Tutup