[feature request] Implementing '...' for Variable Declaration within Scope using Anonymous Functions
まだ誰も着手していません。
評価
調査の方向性
ファイルやテストは指定されていません。まず、変数宣言と匿名関数に関する parser とコンパイルのテストを見つけ、次に Lua の可変長引数がどのように表現されているかを追跡してください。スコープ内の ... 宣言がコンパイルされ、nil 値を含む複数の戻り値が保持され、既存のスコープの動作を壊さないことが完了の条件です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Lua
- スター
- 620
- フォーク
- 45
- 平均マージ
- 9時間 5分
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
IppClub/YueScript のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
IppClub/YueScript の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
follow-up
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
ksysoev/duet.nvim#265 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug: accuracy
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
PathOfBuildingCommunity/PathOfBuilding#10649 ·
メンテナーはふだん 1 日以内に返信
-
reMarkable
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
koreader/koreader#16485 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
nvim-orgmode/orgmode#1213 ·