为什么 'if not s' 和 's ~= nil' 是不同的?

在 Lua 中,我编写了以下代码:

for s in string.gmatch(path, "([^'\\']+)") do
    if not s then -- if s ~= nil then
        table.insert(path_tb, s)
        print(s)
    end
end

如果我将 if not s 替换为 if s ~= nil,我将得到不同的结果。这背后的原因是什么?

点赞
用户107090
用户107090

not s 成立的条件是当 s 为 nil 或 s 为 false。

2018-10-22 11:37:20