Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help,
read this .
Xgoff
Party member
Posts: 211 Joined: Fri Nov 19, 2010 4:20 am
Post
by Xgoff » Tue Jan 31, 2012 8:52 pm
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)
MarekkPie
Inner party member
Posts: 587 Joined: Wed Dec 28, 2011 4:48 pm
Contact:
Post
by MarekkPie » Tue Jan 31, 2012 9:44 pm
The problem is...Lua makes nil return false.
Example:
Tnzu6.png (25.09 KiB) Viewed 301 times
Xgoff
Party member
Posts: 211 Joined: Fri Nov 19, 2010 4:20 am
Post
by Xgoff » Tue Jan 31, 2012 9:56 pm
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
MarekkPie
Inner party member
Posts: 587 Joined: Wed Dec 28, 2011 4:48 pm
Contact:
Post
by MarekkPie » Tue Jan 31, 2012 9:59 pm
Interesting. Just tested and it worked.
bartbes
Sex machine
Posts: 4946 Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:
Post
by bartbes » Tue Jan 31, 2012 10:02 pm
Actually, not nil is true, so it never reaches the third option, re-ordering them should do the trick.
MarekkPie
Inner party member
Posts: 587 Joined: Wed Dec 28, 2011 4:48 pm
Contact:
Post
by MarekkPie » Wed Feb 01, 2012 3:48 am
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
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Wed Feb 01, 2012 8:17 am
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.
MarekkPie
Inner party member
Posts: 587 Joined: Wed Dec 28, 2011 4:48 pm
Contact:
Post
by MarekkPie » Wed Feb 01, 2012 5:15 pm
I just wanted to be verbose.
kikito
Inner party member
Posts: 3153 Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:
Post
by kikito » Wed Feb 01, 2012 9:56 pm
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
When I write def I mean function .
MarekkPie
Inner party member
Posts: 587 Joined: Wed Dec 28, 2011 4:48 pm
Contact:
Post
by MarekkPie » Wed Feb 01, 2012 11:11 pm
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?
)
Users browsing this forum: Ahrefs [Bot] , Bing [Bot] , rabbitboots and 0 guests