Hi, I think the tittle is self explanatory. I always had to fight against that lua lack. I came from c, so I am very accustomed to use the inline conditions. And it can be very useful for shrink code, like with tweetcarts. I know, there are some approaches in lua to solve this, but an official and explicit way would be very good.
> A "a if cond else b" would be very cool.
it's kinda here using the short-if syntax, the condition have to be in parenthesis :
a=true if(a) x="yes" else x="no" ?x a=false if(a) x="yes" else x="no" ?x |
output 'yes' then 'no'.
But I agree a true ternary operator would be insanely useful.
It's required to work when a can be false or null. Bro, what you think it's clever, use and-or for inline conditions, or a ternary operator that every programmer already knows without the need to search in the documentation? Also, the code is more legibble with if-else than with and-or. I just see the ternary as an advantage
"Bro, what you think it's clever" <-- No? I was just pointing out that the Lua language is set up that way. Whether tutorials cover it is a separate matter, as is whatever other paradigms a programmer is used to.
That said, you're requesting a feature that's already covered by the language, but a version with one specific case covered in less tokens. I think your request would make a lot more sense if you showed why that specific case would come up. In particular, can you show an actual situation where both a and b could potentially be nil or false? If b could be, then there's no problem. If a could be, then you can write not c and b or a
instead.
you’ve made your request, no need to insist vehemently. we can discuss or disagree courteously!
if zep thinks this is a real problem and wants to diverge pico8-lua a bit more from stock lua 5.2, he may reply (although this thread is in the wrong category — you can change this).
some notes:
-
I and others were just pointing out the current way to do this.
-
putting the a/b parts inside a table is not common practice, because only false and nil evaluate to false in lua, not empty string or 0 or empty table. (it was different for python, which eventually added an inline ternary operator.)
- «requires you to think about what the line does at first» that’s true for all code
«a easier reading for the beginners» I don’t think there is anything intuitive in programming languages, it’s all constructed and has to be learned!
[Please log in to post a comment]