Macros in 'when' clause don't capture variables
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 48/100
Rechercherichtung
Beginne beim Pfad der Makroexpansion für switch/when-Pattern-Matching und vergleiche ihn mit dem direkten Array-Pattern-Beispiel. Reproduziere den gezeigten $binop-Fall und verfolge, ob Bezeichner nach der Expansion gebunden werden; abgeschlossen bedeutet, dass makrobasierte Patterns a, b und c wie das direkte Pattern erfassen oder dass das Verhalten als beabsichtigt dokumentiert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
I want to have something at least resembling algebraic data types in Yuescript - these are called enums in Rust, variants in OCaml, tagged unions in C. These are types that you can construct and they carry the underlying variant and some fields that the variant has. An example of this in Rust can be seen here: https://doc.rust-lang.org/rust-by-example/custom_types/enum.html
To that end I've been trying to use existing pattern matching functionality and macros to get this behavior. For the sake of example, let's say we want to encode variants of an enum as an array ['VariantName', ...fields], for example:
lang =
BinOp: (lhs, op, rhs) => ['BinOp', lhs, op, rhs]
Var: (name, expr) => ['Var', name, expr]
Const: (val) => ['Const', val]
Then I'd like to do pattern matching on different variants, but have a bit nicer syntax:
macro binop = (x,y,z) -> {
code: "['Binop', #{x}, #{y}, #{z}]"
type: "yue"
}
switch e
when $binop a,b,c
print "foo"
But this compiles to:
if {
'Binop',
a,
b,
c
} == e then
return print("foo")
end
But this isn't what I want, as it does not assign a, b, c variables for me to use inside the 'when' block. If I use the expected output of the macro directly:
switch e
when ['Binop', a, b, c]
print "bar"
I get what I expected - a, b, c are assigned values of e[2..4]:
local _type_0 = type(e)
local _tab_0 = "table" == _type_0 or "userdata" == _type_0
if _tab_0 then
local a = e[2]
local b = e[3]
local c = e[4]
if 'Binop' == e[1] and a ~= nil and b ~= nil and c ~= nil then
return print("bar")
end
end
This behavior is a bit unexpected, seems like macro evaluation happens at some later stage of compilation so instead of variable capture I get table comparison. Is this a bug or working as intended?
- 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 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
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/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 ·