Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arity error with the return of table.unpack #926

Open
fperrad opened this issue Feb 3, 2025 · 2 comments
Open

arity error with the return of table.unpack #926

fperrad opened this issue Feb 3, 2025 · 2 comments

Comments

@fperrad
Copy link
Contributor

fperrad commented Feb 3, 2025

local function foo (a: string, b: string, c: string, d:string, e?: string)
   print(a, b, c, d, e)
end

local t = { 'a', 'b', 'c', 'd' }
assert(#t >= 4)
foo(table.unpack(t))  -- ERROR: wrong number of arguments (given 2, expects at least 4 and at most 5)

foo('bar', table.unpack(t))  -- ERROR: wrong number of arguments (given 3, expects at least 4 and at most 5)

note: #pragma arity on/off is a workaround.

@fperrad
Copy link
Contributor Author

fperrad commented Feb 5, 2025

After more thinking, it is not an issue. I miss a cast.

foo(table.unpack(t) as (string, string, string, string))

But it could be nice to add in the doc/manual a list of functions that their return usually need a cast.

  • pcall
  • string.match
  • string.unpack
  • table.unpack
  • ...

@catwell
Copy link
Contributor

catwell commented Feb 10, 2025

Note that this also works:

local t: {string, string, string, string} = { 'a', 'b', 'c', 'd' }
foo(table.unpack(t))

This makes me think about a few improvements that could be made to tuples, I will open a discussion thread for this.

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

No branches or pull requests

2 participants