Page 2 of 2
Re: How to undraw something?
Posted: Tue Jan 31, 2012 8:52 pm
by Xgoff
tentus wrote:In Lua, you don't have to say == true
usually you don't, but yes you don't need this in general
i've tried ternary logic once or twice with events being called on objects, where you do want to make that distinction sometimes:
- e == true: event ran successfully
- e == false: event exists, but didn't run successfully
- e == nil: event doesn't exist
or without those specific checks then it just collapses into "did happen" or "didn't happen", which is also useful
could also be useful for certain types of configuration settings (true: enable; false: disable; nil: use last/default setting)
Re: How to undraw something?
Posted: Tue Jan 31, 2012 9:44 pm
by MarekkPie
The problem is...Lua makes nil return false.
Example:
- Tnzu6.png (25.09 KiB) Viewed 296 times
Re: How to undraw something?
Posted: Tue Jan 31, 2012 9:56 pm
by Xgoff
MarekkPie wrote:The problem is...Lua makes nil return false.
Example:
my post probably wasn't very clear on it but you would test for true/false/nil explicitly (or at least the latter two) if you needed the distinction
Re: How to undraw something?
Posted: Tue Jan 31, 2012 9:59 pm
by MarekkPie
Interesting. Just tested and it worked.
Re: How to undraw something?
Posted: Tue Jan 31, 2012 10:02 pm
by bartbes
Actually, not nil is true, so it never reaches the third option, re-ordering them should do the trick.
Re: How to undraw something?
Posted: Wed Feb 01, 2012 3:48 am
by MarekkPie
bartbes wrote:Actually, not nil is true, so it never reaches the third option, re-ordering them should do the trick.
Yup, that did it. Now you got the best of both worlds: succinct code and ternary testing.
Code: Select all
function nilTest(a)
if a then
print("true")
elseif a == nil then
print("nil")
elseif not a then
print("false")
end
end
Re: How to undraw something?
Posted: Wed Feb 01, 2012 8:17 am
by Robin
The below is equivalent:
Code: Select all
function nilTest(a)
if a then
print("true")
elseif a == nil then
print("nil")
else
print("false")
end
end
If you can see why, you've got the hang of booleans.
Re: How to undraw something?
Posted: Wed Feb 01, 2012 5:15 pm
by MarekkPie
I just wanted to be verbose.
Re: How to undraw something?
Posted: Wed Feb 01, 2012 9:56 pm
by kikito
Well, succinct is pretty much the contrary of verbose, isn't it?
Little known fact: "nil and true" returns nil, not false. "false and true" returns false. "<Everything else> and true" returns true. With that knowledge, you an do this:
Code: Select all
function nilTest(a)
print(a and true)
end
Re: How to undraw something?
Posted: Wed Feb 01, 2012 11:11 pm
by MarekkPie
I get the "false and true" because of short-circuiting. But that's interesting about nil and true. And makes it even more succinct (or is it verbose? Maybe succbose?
)