lua: a and b or c,b=nil总返回c
2020-02-29 20:42
64 查看
刚开始用lua,在lua里,and or经常被当成?:来用,今天在写的时候就发现个问题:
a and b or c, 假如b=nil,那么总返回c,与a无关,因为实际上是(a and b) or c,b=nil括号里就必然返回false了,但是我想得到的是nil,又不想写ifelse。
要么写成(not a)and c or b,放or后面b就可以为nil,但c不能为nil;要么写成 (a and {b} or {c} )[1],这样似乎猥琐了一点,不知道还有没有更好的写法
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Lua中and、or的一些特殊使用方法
- Lua a and b or c
- lua 逻辑运算 and, or, not【转】
- lua and or not 逻辑运算符
- lua and or not 逻辑运算符
- LUA中的and与or
- LUA中的and与or
- lua and or not 逻辑运算符
- LUA中的and与or
- redis的lua脚本拓展,返回nil及其判断
- LUA中的and与or
- Lua中and、or的一些特殊用法
- lua中逻辑运算符and与or的用法
- LUA中的and与or
- Lua-当unpack遇上条件表达式 ?and ? or ?
- Lua中and、or的一些特殊用法
- lua 之 and or not
- 逻辑操作符---Lua: and,or,not 对比 C++:&&,||,!
- 该怎样理解 Lua 逻辑运算符 and 和 or 的设计?
- Lua中and和or的用法和记忆方法