有没有更简单的方法来编写它而不将其放入函数中?
我几乎每一行都这样做:
if printAll then print("message for debugging") end
有没有一种方法可以语法糖这个,而不将其放入函数中?
local function cPrint(text) --conditional printing
if printAll then
print(text)
end
end
cPrint("message for debugging")
因为该函数必须不断地传递一个变量,然后才发现它是不需要的。由于 printAll
通常为 false,这种资源滥用不是我想要的。这个函数可能每分钟运行数百万次,传递一个仅使用内存而被丢弃的参数感觉很糟糕。
那么有没有一种方法可以在传递参数之前放弃这个条件,这样看起来更好一点呢? (主要原因是,读取以“if”开头的嵌套 if-else 结构会更加混乱。)
I do this nearly every line:
if printAll then print("message for debugging") end
Is there a way to syntax sugar this, without putting it in a function?
local function cPrint(text) --conditional printing
if printAll then
print(text)
end
end
cPrint("message for debugging")
Because that function would have to constantly pass a variable only to find out that it isn't needed. Since printAll
is usually false, this kind of misuse of resources is not what I want. This function might get run millions of times per minute, it just feels bad to pass an argument that uses memory only to be discarded.
So is there a way to discard this condition before an argument is passed that's a little nicer to look at? (The main reason being that it's more confusing to read nested if-else structures with those lines in there that start with "if".)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对你有用吗?
它节省了执行
if
,但不节省函数调用。这是一个替代方案,它执行隐藏的
if
,但避免了函数调用:它实际上并不比
Does this work for you?
It saves executing the
if
, but not the function call.Here is an alternative, that executes a hidden
if
, but avoids the function call:It's not really any more readable than