Page 1 of 1
Ternary operator for just two possibilities?
Posted: Thu Feb 15, 2018 1:46 pm
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
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:
Is there a way to do it like this or indeed any other way to write it shorter?
Re: Ternary operator for just two possibilities?
Posted: Thu Feb 15, 2018 2:20 pm
by grump
hasen wrote: ↑Thu Feb 15, 2018 1:46 pm
But clearly this didn't work:
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
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
Re: Ternary operator for just two possibilities?
Posted: Thu Feb 15, 2018 2:21 pm
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.
Re: Ternary operator for just two possibilities?
Posted: Thu Feb 15, 2018 2:50 pm
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.
Re: Ternary operator for just two possibilities?
Posted: Thu Feb 15, 2018 7:36 pm
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.
Re: Ternary operator for just two possibilities?
Posted: Fri Feb 16, 2018 3:18 am
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
Re: Ternary operator for just two possibilities?
Posted: Fri Feb 16, 2018 2:19 pm
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.
Re: Ternary operator for just two possibilities?
Posted: Fri Feb 16, 2018 2:20 pm
by hasen
Ok looks interesting, what do you mean by let this happen though? I thought it's already working.
Re: Ternary operator for just two possibilities?
Posted: Sat Feb 17, 2018 1:18 am
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.
Re: Ternary operator for just two possibilities?
Posted: Sat Feb 17, 2018 2:12 am
by zorg
other than what metatables offer (which isn't much), using metaprogramming with löve may be harder than what's it worth.