if not "a" == "b"情况下的优先级

Prescedence in the case of if not "a" == "b"

function test()
  if not "a" == "b" then
    print("the strings are different")
  end
end

似乎我的文件中到处都是这个错误。 Lua 将 not 拉得比 == 强,并解释为我在问 "a" 是否为零值。我知道 not 的优先级高于 ==,但是 Lua 怎么能忽略该行的其余部分呢?难道它不应该注意到那里有更多的东西并抛出 == "b" 不符合语法的错误吗?

您可以使用 logical operators 组合任意数量的值。除非您使用括号 Lua 将使用运算符优先级来确定首先评估的内容。

Lua pulls the not stronger than the ==, and interprets that I'm asking whether "a" is a nil value. I understand that not has higher precedence than ==

正确。这就是为什么 Lua 将 not "a" == "b" 计算为 (not "a") == "b" 并解析为 false == "b" 并最终解析为 false.

如果要检查两个值是否不相等,请使用不等于运算符 ~=

Shouldn't it notice that there's more coming there and throw an error that == "b" doesn't fit into the syntax?

否,因为这是正确的语法。你只是使用了一个没有得到你想要的表达式。

What about the case not ("a" == "b" or "c" == "d")? Will these require nested parenthesis, i.e. does it apply the not in the same way as above once in the parenthesis?

not ("a" == "b" or "c" == "d") 解析为 true

or 的优先级最低。所以你不需要进一步的括号。您可能应该使用更实际的示例,因为这些字符串永远不会相等。

(I don't want to do x ~= y, because I'm comparing objects where I'm not entirely sure they have a negative comparison.)

我无法理解这个。