Hey, guys.
Does Lua have a quick fix for the ternary operator?
Or do I need to set some metatables for it?
Ternary Operator
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Ternary Operator
i don't understand your question
wath fix do you speak about ?
the fact that don't work as expected ?
wath fix do you speak about ?
the fact that
Code: Select all
v = v and false or true
Current work : Isömap
Re: Ternary Operator
Well, it looks like that only returns true or false upon testing v.Saegor wrote:the fact thatdon't work as expected ?Code: Select all
v = v and false or true
I need a operator that can execute a piece of code if something is true else, execute another piece of code.
Re: Ternary Operator
in fact it don't work because the second argument is false. it's the main problema with ternary operators in LuaRaylin wrote:Well, it looks like that only returns true or false upon testing v.Saegor wrote:the fact thatdon't work as expected ?Code: Select all
v = v and false or true
I need a operator that can execute a piece of code if something is true else, execute another piece of code.
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
else, explain maybe more accurately your problem
Current work : Isömap
Re: Ternary Operator
I'm trying to emulate the same functionality that the question mark ((test)?a:b) would give.
Re: Ternary Operator
Have you tried this? I'm not sure what is so difficult.
or
Code: Select all
v = (v and func1() or func2()) and v
Code: Select all
if v then
...
else
...
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Ternary Operator
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.Raylin wrote:I'm trying to emulate the same functionality that the question mark ((test)?a:b) would give.
Help us help you: attach a .love.
Re: Ternary Operator
Thanks, guys. Robin's answer pretty much sealed the deal.
Who is online
Users browsing this forum: No registered users and 4 guests