I am trying to use this function in Lua 4:
function ternary(cond, T, F)
if cond then return T else return F end
end
In this context:
loadHW1 = false
print(ternary(loadHW1 == true, "this should not appear", nil))
However, the text always gets printed when instead I expect the result to be nil
. What am I doing wrong? Thanks.
[ed]
I switched to this but still get a "this is true" result:
loadHW1 = 0
print(ternary(loadHW1, "this is true", "this is false"))
Lua 4 doesn't have Booleans: they were introduced in Lua 5.
In Lua 4, only nil is false; anything else, including 0, is true.
In your code,
false
and true
are being interpreted as undefined global variables and so both evaluate to nil. Therefore, loadHW1 == true
becomes nil == nil
, which is true, and so ternary
receives 1 for cond
.
If you want to use false
and true
in Lua 4, define them as follows:
false = nil
true = 1