proposal: explicit control of multiple value adjustment

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

proposal: explicit control of multiple value adjustment

Viacheslav Usov
Currently, Lua has strict implicit rules with respect to the adjustment of multiple values: they are not adjusted if they are the last or the only element in a list of expressions, and adjusted to to one value in every other case.

If, for example, one would like to forward just two values from a function returning more than two, one has to use intermediate variables:

local a, b = foo()
bar(a, b)

One can also not pass multiple values and then some other values to another function:

qux(foo(), bar()) -- no way to pass multiple return values from both foo and bar

I propose adding a way for the user to say "I want to adjust to this number values" and "I want to pass all of them". While syntax is debatable as ever, something like this could work:

For function calls, allow an optional "adjuster" in the form :n, where n is either a number or *, meaning "n values" and "all values" correspondingly. Then the examples above could be re-written as follows:

bar(foo():2)
qux(foo():*, bar())

For vararg expressions, a similar adjuster could be used, even though it looks quite strange, so better syntax is probably possible:

bar(...:2)
qux(...:*, bar())

An open question here is whether the adjuster should be static or dynamic, in the latter case the n being a variable.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: proposal: explicit control of multiple value adjustment

Michal Kottman-2
On 5 July 2017 at 11:32, Viacheslav Usov <[hidden email]> wrote:
I propose adding a way for the user to say "I want to adjust to this number values" and "I want to pass all of them". While syntax is debatable as ever, something like this could work:

For function calls, allow an optional "adjuster" in the form :n, where n is either a number or *, meaning "n values" and "all values" correspondingly. Then the examples above could be re-written as follows:

bar(foo():2)
qux(foo():*, bar())

For vararg expressions, a similar adjuster could be used, even though it looks quite strange, so better syntax is probably possible:

bar(...:2)
qux(...:*, bar())

A slightly modified version of your proposal could be achieved without language change by extending select() to allow a table as the first argument, selecting which arguments you want to pick:

select({1,2}, foo())

This is a bit more generic than your proposal, since it also allows you to reorder arguments:

select({2,1}, foo())

But at the same time it does not allow for passing multiple arguments *after* the "adjusted" ones:

qux(select({1,2}, foo()), bar()) -- no language change, would still pass first value from foo()
Reply | Threaded
Open this post in threaded view
|

Re: proposal: explicit control of multiple value adjustment

Duncan Cross
In reply to this post by Viacheslav Usov
On Wed, Jul 5, 2017 at 10:32 AM, Viacheslav Usov <[hidden email]> wrote:
> I propose adding a way for the user to say "I want to adjust to this number
> values" and "I want to pass all of them".

I made a related proposal a few years ago, where adding an ellipsis
"..." immediately before a comma (or a semicolon separating two values
in an array-style table literal) to retain the full value list instead
of truncating it, for example:

--
local function multival()  return 1, 2, 3  end

call(multival()..., 4, 5, 6)
local a,b,c,d,e,f = multival()..., 4, 5, 6
t = {multival()...; 4; 5; 6}
--

...in each case the full list would be 1, 2, 3, 4, 5, 6. Of course the
syntax gets a little weird when used with the OTHER use of "...", for
example to have a function that returns whatever arguments are passed
to it with one additional value appended:

--
function appendo(...)
  return ... ..., "appended"
end
print(appendo("the", "final", "value", "is"))
--

It also doesn't include the option to fix the exact number of values,
as your proposal does.

-Duncan