Ternary operator for just two possibilities?

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
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Ternary operator for just two possibilities?

Post by hasen »

I read the blog post on the Love2d blog about ternary operators and so wanted to try but couldn't get it to work with the two variables I wanted to switch between. The blog talks about

Code: Select all

x = a and b or c
But I wanted to write this function in shorthand since it was such a quick code but the variable names were long:

Code: Select all

if variable == 1 then variable = 2 else variable = 1 end
But clearly this didn't work:

Code: Select all

variable = 1 and 2 or 1
Is there a way to do it like this or indeed any other way to write it shorter?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Ternary operator for just two possibilities?

Post by grump »

hasen wrote: Thu Feb 15, 2018 1:46 pm But clearly this didn't work:

Code: Select all

variable = 1 and 2 or 1
1 is a constant that is neither false nor nil, thus the expression on the right side of the assignment will always evaluate to 2. It doesn't really make sense to use a constant as the first argument of the operator.
Is there a way to do it like this or indeed any other way to write it shorter?
Your code can be written as

Code: Select all

variable = 2
But you probably wanted to write

Code: Select all

variable = variable == 1 and 2 or 1
That's equivalent to your code

Code: Select all

if variable == 1 then variable = 2 else variable = 1 end
Last edited by grump on Thu Feb 15, 2018 2:22 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Ternary operator for just two possibilities?

Post by raidho36 »

You check against "1", not "variable", so it doesn't work. Note that this is not a real ternary operator, it's just the way Lua's "and" and "or" work allows to make things like that. It will fail to work if your first value resolves to "false" and will return the second value instead.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Ternary operator for just two possibilities?

Post by hasen »

raidho36 wrote: Thu Feb 15, 2018 2:21 pm You check against "1", not "variable", so it doesn't work.
Yes that's why I said it clearly doesn't work. There didn't seem to be a way but Grump has provided a solution.
raidho36 wrote: Thu Feb 15, 2018 2:21 pm Note that this is not a real ternary operator, it's just the way Lua's "and" and "or" work allows to make things like that. It will fail to work if your first value resolves to "false" and will return the second value instead.
Yes I realise there are no real ternary operators in lua, otherwise it would be simple.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Ternary operator for just two possibilities?

Post by Nixola »

If you're just toggling that variable between 1 and 2, you may want to use true and false instead if it makes sense in its context.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
coffeecat
Prole
Posts: 29
Joined: Sun Sep 13, 2015 4:10 pm

Re: Ternary operator for just two possibilities?

Post by coffeecat »

You can use some meta-programming to let this happen. I haven't tried myself, but you might be interested: http://lua-users.org/wiki/MetaProgramming
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Ternary operator for just two possibilities?

Post by hasen »

Nixola wrote: Thu Feb 15, 2018 7:36 pm If you're just toggling that variable between 1 and 2, you may want to use true and false instead if it makes sense in its context.
Yeah of course but unfortunately it doesn't in this case since they were actually levels. There are only two right now so that was just some temp code to switch between them. Looking at the code annoyed me 'cos it was so long and I hate any code that looks long. Especially since it was only temporary. So it was good that I could at least shorten it down some.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Ternary operator for just two possibilities?

Post by hasen »

coffeecat wrote: Fri Feb 16, 2018 3:18 am You can use some meta-programming to let this happen. I haven't tried myself, but you might be interested: http://lua-users.org/wiki/MetaProgramming
Ok looks interesting, what do you mean by let this happen though? I thought it's already working.
coffeecat
Prole
Posts: 29
Joined: Sun Sep 13, 2015 4:10 pm

Re: Ternary operator for just two possibilities?

Post by coffeecat »

I mean you can use meta-programming to get a new syntax like "variable = newvalue ifeq oldvalue". It's definitely a tradeoff to take, as you'll be introducing meta-programming into your project, and that comes with a cost. I personally am using Moonscript (for room scripts only), and don't use any meta-programming in Lua. I do like meta-programming much when I am using Lisp languages.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Ternary operator for just two possibilities?

Post by zorg »

other than what metatables offer (which isn't much), using metaprogramming with löve may be harder than what's it worth.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests