Ternary Operator

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.
Post Reply
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Ternary Operator

Post by Raylin »

Hey, guys.
Does Lua have a quick fix for the ternary operator?
Or do I need to set some metatables for it?
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Ternary Operator

Post by Saegor »

i don't understand your question

wath fix do you speak about ?

the fact that

Code: Select all

v = v and false or true
don't work as expected ?
Current work : Isömap
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: Ternary Operator

Post by Raylin »

Saegor wrote:the fact that

Code: Select all

v = v and false or true
don't work as expected ?
Well, it looks like that only returns true or false upon testing v.
I need a operator that can execute a piece of code if something is true else, execute another piece of code.
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Ternary Operator

Post by Saegor »

Raylin wrote:
Saegor wrote:the fact that

Code: Select all

v = v and false or true
don't work as expected ?
Well, it looks like that only returns true or false upon testing v.
I need a operator that can execute a piece of code if something is true else, execute another piece of code.
in fact it don't work because the second argument is false. it's the main problema with ternary operators in Lua

for your problem, what do you think of

Code: Select all

function love.update()
some_function(condition and "instruct_1" or "instruct_2")
end


function some_function(instruct)

if instruct == "instruct_1" then
...
end

elseif instruct == "instruct_2" then
...
end
end
ps : some issues with firefox, no more tabbing :huh:

else, explain maybe more accurately your problem
Current work : Isömap
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: Ternary Operator

Post by Raylin »

I'm trying to emulate the same functionality that the question mark ((test)?a:b) would give.
User avatar
markgo
Party member
Posts: 189
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Ternary Operator

Post by markgo »

Have you tried this? I'm not sure what is so difficult.

Code: Select all

v = (v and func1() or func2()) and v
or

Code: Select all

if v then
...
else
...
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Ternary Operator

Post by Robin »

Raylin wrote:I'm trying to emulate the same functionality that the question mark ((test)?a:b) would give.
In Lua, that would be test and a or b, but only if a is always a truthy value. If you can't guarantee that, you're stuck using if-statements.
Help us help you: attach a .love.
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: Ternary Operator

Post by Raylin »

Thanks, guys. Robin's answer pretty much sealed the deal. :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests