【发布时间】:2023-04-10 16:04:01
【问题描述】:
我已经在网上和其他地方搜索过答案,但所有主题都涉及迭代表、元表或_,var1 = do_some_stuff() 的时间,而这里不是这种情况。
这只是一个不现实的功能,但包含我的意思的示例:
function do_some_stuff(data)
_ = some_function(data)
some_other_code
_ = some_other_function(data)
end
这不就等同于直接输入吗:
function do_some_stuff(data)
some_function(data)
some_other_code
some_other_function(data)
end
我知道,如果我创建一个这样的基本 Lua 程序,两个版本的运行方式相同:
function hello(state)
print("World")
end
function ugh(state)
_ = hello(state) -- and w/ hello(state) only
end
ugh(state)
我只是想知道是否有必要使用此_ = some_function()?
【问题讨论】:
-
你看到
_ = some_function()还是看到_, val = some_function()?因为后者实际上意味着什么并且是必要的。 -
@EtanReisner 你是完全正确的,我理解我会使用后者的时候。不,情况并非如此,但我会根据您的问题进行更新。谢谢!
-
我想不出
_ = some_function()完全有意义的情况(这不是perl),但我不能完全肯定地说这是没有意义的(尤其是因为我没有'根本不关注 lua 5.2 或 5.3)。 -
@EtanReisner 感谢 Etan。从我到目前为止的尝试来看,我没有看到任何区别。但我绝不像这里的许多成员那样知识渊博或经验丰富。谢谢
-
setmetatable(_G,{__newindex = function(_,n, v) if n == '_' then print('...') end end}):) 我认为唯一的副作用是覆盖全局/上值。我看到使用下划线作为合法值的代码。
标签: variables lua metasyntactic-variable