Lua中的感叹号有什么作用? [英] What does an exclamation mark in Lua do?
问题描述
问题确实在标题中.我看到有人较早使用它,但我不知道!
的用途.
Question is in the title, really. I saw someone use this earlier and I didn't know what the !
was used for.
local lowestIndex = 0;
local lowestValue = false;
for k, v in ipairs(playerElement) do
if !lowestValue or v.value < lowestValue then
lowestIndex = k;
lowestValue = v;
end
end
推荐答案
正如其他人所说,!
在Lua中通常不起作用,并且您发布的代码通常无效.但是,扩展Lua的解析器以允许自定义语法是相当琐碎的,对于嵌入Lua的项目,除了not var
之外,它还添加了!var
和!=
等更熟悉的" C样式语法,这并不是闻所未闻的.和~=
. Garry's Mod ,并且我确定还有其他人.
As others have said, !
normally has no function in Lua, and the code you posted would not normally be valid. However, it's quite trivial to extend Lua's parser to allow for custom syntax, and it's not unheard of for projects which embed Lua to add "more familiar" C-style syntax such as !var
and !=
in addition to not var
and ~=
. One notable project which does this is Garry's Mod, and I'm sure there are others.
当然,如果可能的话,最好避免使用普通语法(或首先对其进行自定义)时使用自定义语法,以免完全避免这种混淆.
Of course, using custom syntax when the normal syntax is available (or customising it in the first place) is best avoided, if possible, to avoid exactly this sort of confusion.
这篇关于Lua中的感叹号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!