为什么Lua会像它那样进行比较?

 恩和佳蓉伟诚 发布于 2023-02-11 16:29

如果标题没有多大意义,请原谅我.我不得不选择Is Lua的比较方式有用吗?Lua的比较.

我今天想做这样的事情:

 if currChar == nextChar == "-" then
    ... 
 end

但它false每次都会回来:

> currChar="-"
> nextChar="-"
> =currChar == nextChar == "-"
false
>
-- All true in Python
print(5 == 5)                    -- true
print(5 == 5 == 5)               -- false
print((5 == 5) == (5 == 5))      -- true
print(5 == (4 + 1) == (6 - 1))   -- false

我在一段时间内摆弄了这些值,并发现由于某种原因,Lua从左到右成对地比较值:

> = 52 > 3 > 2
stdin:1: attempt to compare number with boolean
stack traceback:
        stdin:1: in main chunk
        [C]: in ?
>

我有一种情况,这种形式的比较是有用的吗?
为什么比较那样?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有