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

Offen
#144 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
lua
Bereich
compilers

Rechercherichtung

Es werden keine Dateien oder Tests genannt. Beginne damit, die Parser- und Kompilierungstests für Variablendeklarationen und anonyme Funktionen zu finden, und verfolge anschließend, wie Lua-Varargs dargestellt werden. Die Aufgabe ist abgeschlossen, wenn Deklarationen von ... innerhalb eines Gültigkeitsbereichs kompiliert werden und mehrere Rückgabewerte einschließlich nil-Werten erhalten bleiben, ohne das bestehende Verhalten von Gültigkeitsbereichen zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
Lua
Sterne
620
Forks
45
Ø Merge
9 Std. 5 Min.
Gemergte PRs (30 T.)
1

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus IppClub/YueScript

Alle Issues in IppClub/YueScript

Ähnliche Issues

Weitere Issues zu Lua

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.