General discussion about LÖVE, Lua, game development, puns, and unicorns.
mirage
Prole
Posts: 8 Joined: Thu Sep 01, 2011 7:59 pm
Post
by mirage » Thu Sep 08, 2011 1:02 am
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?
vrld
Party member
Posts: 917 Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:
Post
by vrld » Thu Sep 08, 2011 1:08 am
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
mirage
Prole
Posts: 8 Joined: Thu Sep 01, 2011 7:59 pm
Post
by mirage » Thu Sep 08, 2011 1:18 am
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
Post
by Rad3k » Thu Sep 08, 2011 1:23 am
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.
vrld
Party member
Posts: 917 Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:
Post
by vrld » Thu Sep 08, 2011 1:24 am
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
mirage
Prole
Posts: 8 Joined: Thu Sep 01, 2011 7:59 pm
Post
by mirage » Thu Sep 08, 2011 1:29 am
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.
Thanks so much!
Users browsing this forum: No registered users and 5 guests