[feature request] Implementing '...' for Variable Declaration within Scope using Anonymous Functions
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
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
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus IppClub/YueScript
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 64/100
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 35/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
Alle Issues in IppClub/YueScript
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 2 Tagen
-
follow-up
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
ksysoev/duet.nvim#265 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug: accuracy
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
PathOfBuildingCommunity/PathOfBuilding#10649 ·
Maintainer antworten meist innerhalb von 1 Tag
-
reMarkable
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
koreader/koreader#16485 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
nvim-orgmode/orgmode#1213 ·