IppClub / IppClub/YueScript

[feature request] Implementing '...' for Variable Declaration within Scope using Anonymous Functions

Open
#144 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
613
Forks
45
PR merge metrics
No merged PRs in 30d

Description

Hi,
In the Lua language, the ellipsis ... can only be used in the last function argument.

local function a(...)
    print(...)
end

It is not possible to declare it in the scope (e.g., as local ... = 1, 2).

local ok, ... = true, true -- syntax error! ('<name>' expected near '...')

My suggestion is to implement the use of ... to declare it in the scope, using anonymous functions for that purpose. During parsing, if there is ... in the variable declaration, Yuescript can wrap that scope inside a function and call it with the values. This way, ... will be defined within the same scope.

list = {1, 2, 3, 4, 5}
fn = (ok) ->
  ok, table.unpack list
ok, ... = fn true
print ok, ...
ok, ...

compiles to:

local list = {
  1, 
  2,
  3,
  4,
  5
}
local fn
fn = function(ok)
  return ok, table.unpack(list)
end
return (function(ok, ...)
    print(ok, ...) -- "ok" and "..." is available here without errors!
    return ok, ...
end)(fn(true))
Notes

In my scripts, I use varargs (...) returns frequently. Usually, they contain 'nil' (holes), so I cannot obtain the correct length if I wrap them in tables '{...}'. It is necessary to return the length and then use 'table.unpack'

local function fn_many_args()
    return 10, nil, 20, nil, 30
end

local args = {fn_many_args()}
print(#args) --> 1

local _ = (function(...)
    print(select("#", ...)) --> 5
    print(...) --> 10, nil, 20, nil, 30
end)(fn_many_args())


-- I can pass the function to select:
print(select("#", fn_many_args())) --> 5
-- But, How can I get the items? I will need call the function twice!
print(select(1, fn_many_args())) --> 10, nil, 20, nil, 30
-- First to get length, and second to catch the items

Regards

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by locating the parser and compilation tests for variable declarations and anonymous functions, then trace how Lua varargs are represented. Done means scoped ... declarations compile and preserve multiple returns, including nil values, without breaking existing scope behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.