Skip to content

v2: space-lua: Implement select(index, ...) and align behaviour of the # operator with Lua 5.4 #1401

Open
@mjf

Description

@mjf

The Lua implementation is missing the select(index, ...) [1] function...

${select(1, "a", "b", "c")}

should yield: a but it instead yields and error: "Lua error: Attempting to call nil as a function".

It's very limiting issue because without the select(index, ...) functions like the following one simply cannot be implemented (not tested in SB, works in normal Lua)...

function coalesce(...)
  local argn = select('#', ...)

  if argn == 0 then
    return nil
  end

  local argv = { ... }

  for i = 1, argn do
    if argv[i] ~= nil then
      return argv[i]
    end
  end

  return nil
end

Note: The select(index, ...) is the most performant and often used way to get number of variadic arguments of a function: select('#', ...). Therefor it should not be (and, in fact, it cannot be) implemented as interpreted in the Lua itself but rather be part of the embedded Lua core, similarly to the other core Lua functions. The select(index, ...) even cannot be implemented in interpreted Lua because the # operator and similar may have some special properties (e.g., the #{...} stops counting on nil values etc.). So the only way to iterate properly over ... incuding nil values is the select(index, ...) function. I ask for implementing the select(index, ...) directly in the core of the Lua interpreter because it's rather part of the language syntax than anything else.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions