lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Lua has multiple returns, but no way to use them inline with a function call. I was wondering if something like these could be added:

First idea: `f([3]pairs(t), 1, 2, 3)`;

- +This syntax is currently illegal, so it wouldn't break old programs.
- +The `[3]` clearly indicates how many return values to transpose into arguments. - +Can limit varargs: with `f([2]pairs(t))`, `f` would only see the first 2 values of the `pairs(t)` call, dropping the 3rd. - -Makes it feel like you can use `f([x]pairs(t))` (variable x) when in fact you can't.

Second idea: `f(pairs(t),,, 1, 2, 3)`;

- +This syntax is also illegal, so it also wouldn't break old programs.
- +While not obvious at first, the number of commas after the function call indicate how many return values to transpose into arguments. - Commas at the end of the call would still be illegal, this has the caveat of not being able to limit varargs. - Could be made so a single comma at the end means 2 arguments, as we can already use `(f())` to limit the results to a single value. The caveat is that commas at the end would mean ONE LESS than the number of arguments. - ?This conflicts with the "implicit nil" patch, as the implicit nil patch makes `f(pairs(t),,,1)` become `f(pairs(t),nil,nil,1)`. - HOWEVER, this is more of an extension to the "implicit nil" patch: the code `f(t,,,1)`, would become `f(t,nil,nil,1)`.
- -(Very) Prone to "silent" typos.

The former syntax is better than the latter (3 positive points and 1 negative for the former, 2 positive and 1 negative for the latter), altho I expect more people to like the latter simply because it's similar to implicit nil.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.