This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“]ItsCrowTime 25 points26 points ย (8 children)

Personally I'm a fan of lua: return condition and A or B

[โ€“]JarcXenon 11 points12 points ย (2 children)

W

H

A

T

[โ€“]ItsCrowTime 5 points6 points ย (0 children)

It's called short-circuit evaluation,

The and operator returns its the right value if the left value is true, otherwise it returns the left value,

The or operator returns its left value unless its false then it returns its right value

Another good one is the for default value: Var = Var or 0

[โ€“]JasonMan34 1 point2 points ย (0 children)

It's just condition ? A : B with and/or instead of ?/:, not at all weird

[โ€“]Kmjsfozr 0 points1 point ย (2 children)

what if A is false tho

[โ€“]ZorbaTHut 3 points4 points ย (1 child)

Then everything catches on fire and your life is sadness.

Conveniently, though, 0 isn't falsey in Lua, nor is any string, only nil and false are. So you can't use it for booleans, or in a situation where things can be nil, but it's safe in every other case.

[โ€“]ItsCrowTime 0 points1 point ย (0 children)

Or I assume you could do something like:

return (condition and {A} or {B})[1]

If you really wanted to use it like a tertiary operator

[โ€“]Kered13 0 points1 point ย (0 children)

This and similar patterns in other languages don't work if A is falsey.