Skip to content

Commit 1f6753e

Browse files
committed
main
1 parent 35f03f8 commit 1f6753e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Control Flow
2+
3+
## Ternary Operator
4+
5+
**`and .. or` statement can be a replacement of ternary operator, but the trap here is, the expression after `and` must not ever return a falsy value.**
6+
That is because the statment is not terminated when `cond` was met, it falls to expression after `or`.
7+
8+
```lua
9+
-- first eval cannot be nil
10+
local fn = function()
11+
if cond then
12+
return true
13+
end
14+
return nil
15+
end
16+
17+
_ = cond and fn() or canbe_nil -- canbe_nil might be picked when fn() returns falsy value which is unexpected
18+
```

0 commit comments

Comments
 (0)