Short IF Statement in Lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
mirage
Prole
Posts: 8
Joined: Thu Sep 01, 2011 7:59 pm

Short IF Statement in Lua

Post by mirage »

In Objective-C I could take something like
if (foo){
print("true");
}else{
print("false");
}
and turn it into
print( (foo?"true":"false") );
Does Lua have anything similar to this?
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Short IF Statement in Lua

Post by vrld »

Yes: and and or work on any values, not just booleans:

Code: Select all

if foo then print("true") else print("false") end
-- equal to
print(foo and "true" or "false")
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
mirage
Prole
Posts: 8
Joined: Thu Sep 01, 2011 7:59 pm

Re: Short IF Statement in Lua

Post by mirage »

I'm noticing that when I try to concatenate a string to this, it doesn't work

Code: Select all

print("1st run: " ..(initialRun and "Yes" or "No"))
Without the parentheses, an error is thrown for string to boolean concatenation

With the parentheses, initialRun returns TRUE no matter what.
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Short IF Statement in Lua

Post by Rad3k »

mirage wrote:I'm noticing that when I try to concatenate a string to this, it doesn't work

Code: Select all

print("1st run: " ..(initialRun and "Yes" or "No"))
Without the parentheses, an error is thrown for string to boolean concatenation

With the parentheses, initialRun returns TRUE no matter what.
Strange, because your code works well for me.
And without parentheses, the concatenation operator gets precedence over logical ones, so it tries to concatenate "1st run:" with initialRun value (which is probably a boolean in your case).
Last edited by Rad3k on Thu Sep 08, 2011 1:26 am, edited 1 time in total.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Short IF Statement in Lua

Post by vrld »

Works for me:

Code: Select all

> initialRun = false
> print("1st run: " ..(initialRun and "Yes" or "No"))
1st run: No
> initialRun = true 
> print("1st run: " ..(initialRun and "Yes" or "No"))
1st run: Yes
mirage wrote:Without the parentheses, an error is thrown for string to boolean concatenation
You should take another look at Programming in Lua chapter 3: Expressions, especially section 3.5: Precedence.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
mirage
Prole
Posts: 8
Joined: Thu Sep 01, 2011 7:59 pm

Re: Short IF Statement in Lua

Post by mirage »

I found the problem. I was expecting "love.filesystem.mkdir()" to return false if the folder already existed. I will need to use "love.filesystem.exists()" for that.

:neko: Thanks so much! :ultraglee:
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests